When we open a new file using the FILE structure and fopen(), the file pointer points to the first byte of the file. How can we know the location of any byte in the file using the file pointer? When I try to store the location of fp (file pointer) at a desirable location it gives me an absolute offset and not the offset which is from the starting of the file i.e 00. How do I know the location of any byte in a file from the file's first byte?

Recommended Answers

All 2 Replies

When I try to store the location of fp (file pointer) at a desirable location

How exactly are you doing this? While FILE is supposed to be an opaque type, it's generally implemented as a structure. If you're storing the pointer to a FILE then that's meaningless. If you're storing some serialization of the structure then that's largely meaningless as well (not to mention non-portable).

How do I know the location of any byte in a file from the file's first byte?

You can find the position within the file using ftell() or fgetpos(). Note that I didn't say that this is the location of any byte, because these functions return an encoded offset rather than a raw byte offset. It only matters when the file is opened in text mode, and even then only when newline characters are more than one byte on the platform (eg. Microsoft Windows).

But if I'm understanding your question correctly, this is what you want.

Hey thanks again! Yes, this is what I was looking for. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.