0

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?

2
Contributors
2
Replies
19
Views
4 Years
Discussion Span
Last Post by anumash
Featured Replies
  • 1

    > 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 … Read More

1

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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.