int fsetpos(FILE *stream, const fpos_t *pos);
reset the position indicator to using fsetpos() stores the current file position indicator to the location indicated by the information in pos Returns non-zero on error.
#include <stdio.h> /* including standard library */ //#include <windows.h> /* uncomment this for Windows */ int main( void ) { FILE *stream; fpos_t file_pos; int c; int 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("%c", c); } printf("1st Position of # is the %d Byte\n", file_pos); printf("\nset to given position\nand print it again\n"); fsetpos(stream, &file_pos); while((c = fgetc(stream) ) != EOF) { printf("%c", c); } fclose(stream); }
output: ./fsetpos test 1234, ... # ... this is a test for code-reference.com 1st Position of # is the 17 Byte set to given position and print it again ... this is a test for code-reference.com