• C

offset while reading file

I am trying to read something from a bitmap and want to read the info at particular offset from the beginning of file. how can i do that?
I tried this but doesn't seem to work:

FILE *f = fopen("filename");
fread(&somestruct, 1, bytes_to_read, f+someOffset);

is there something similar to seekp() (C++)

Who is Participating?
sneeuw_chanConnect With a Mentor Commented:
Yup, there is.  It's called fseek().
To be exact: int fseek(FILE *stream, long offset, int whence);
('stream' and 'offset' should be obvious and 'whence' is one of 'SEEK_SET, SEEK_CUR or SEEK_END.)
jhshuklaAuthor Commented:
could you explain the meanings of SET, CUR and END? As far as I remember
SET sets the pointer at the offset,
CUR displaces the pointer from the current position by offset bytes
END makes it point at offset bytes (backwards) from the END
Almost.  END makes it point at offset bytes _forwards_ from the end.  Remember, the offset is signed, so it can be negative.
The rest is as you stated, SET is relative to the start of the file, and CUR is relative to the current position.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

jhshuklaAuthor Commented:
do you mean fseek(somefile, +10, SEEK_END) would make is point to ten bytes beyond the EOF? that is a bad thing!
Yup, 10 bytes beyond the EOF.  Why would that be a bad thing ?
jhshuklaAuthor Commented:
not if we know what we are doing. thx.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.