int fgetpos(FILE *stream, fpos_t *pos);
Stores current file position for stream stream in *pos. Returns non-zero on error.
The fgetpos() function stores the file position indicator of the given file stream in the given position variable.
The position variable is of type fpos_t (which is defined in stdio.h) and is an object that can hold every possible position in a FILE.
fgetpos() returns zero upon success, and a non-zero value upon failure.
/* * fgetpos example code * http://code-reference.com/c/stdio.h/fgetpos */ #include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ int main( void ) { FILE *stream; fpos_t file_pos; int c,i; if((stream=fopen("test.txt","r"))==NULL) { printf("Cannot open file.\n"); return 1; } fgetpos(stream, &file_pos); while((c = fgetc(stream) ) != EOF) { if ( c == '#' ){ fgetpos(stream, &file_pos); } printf("character is: %c\n", c); } printf("1st Position of # is the %d Byte\n", file_pos); fclose(stream); return 0; }
output: ./fgetpos character is: L character is: i character is: n character is: e character is: character is: 1 character is: character is: # .... and so on ... 1st Position of # is the 24 Byte