| << Prev | Beej's Guide to C | Next >> |
Get the current position in a file, or set the current position in a file. Just like ftell() and fseek() for most systems.
#include <stdio.h> int fgetpos(FILE *stream, fpos_t *pos); int fsetpos(FILE *stream, fpos_t *pos);
These functions are just like ftell() and fseek(), except instead of counting in bytes, they use an opaque data structure to hold positional information about the file. (Opaque, in this case, means you're not supposed to know what the data type is made up of.)
On virtually every system (and certainly every system that I know of), people don't use these functions, using ftell() and fseek() instead. These functions exist just in case your system can't remember file positions as a simple byte offset.
Since the pos variable is opaque, you have to assign to it using the fgetpos() call itself. Then you save the value for later and use it to reset the position using fsetpos().
Both functions return zero on success, and -1 on error.
char s[100]; fpos_t pos; fgets(s, sizeof(s), fp); // read a line from the file fgetpos(fp, &pos); // save the position fgets(s, sizeof(s), fp); // read another line from the file fsetpos(fp, &pos); // now restore the position to where we saved
| << Prev | Beej's Guide to C | Next >> |