fseek() function in c programming is used to write data into desired file with the help of pointer which help to find the location into the given file.
|
Example Program for FSEEK() function in c programming
|
Syntax :-
int fseek(FILE *stream, long int offset, int whence)
There are three constants are used in fseeks() function in c SEEK_SET, SEEK_CUR and SEEK_END
#include <stdio.h>
void main(){
FILE *fp;
fp = fopen("myfile.txt","w+");
fputs("This is functioinc", fp);
fseek( fp, 7, SEEK_SET );
fputs("Adam stiffman", fp);
fclose(fp);
}
Now let see the Example Program for FSEEK() function in c
#include <stdio.h>
int main ()
{
FILE *f;
char data[60];
fp = fopen ("test.c","w");
fputs("functioinc.in is a programming tutorial website", f);
fgets ( data, 60, fp );
printf(Before fseek - %s", data);
// To set file pointet to 21th byte/character into the file
fseek(f, 21, SEEK_SET);
fflush(data);
fgets ( data, 60, fp );
printf("After SEEK_SET to 21 - %s", data);
// To find backward 10 bytes
fseek(f, -10, SEEK_CUR);
fflush(data);
fgets ( data, 60, fp );
printf("After SEEK_CUR to -10 - %s", data);
// To find 7th byte before the end of file
fseek(f, -7, SEEK_END);
fflush(data);
fgets ( data, 60, f );
printf("After SEEK_END to -7 - %s", data);
// now set file pointer to the start of the file
fseek(f, 0, SEEK_SET);
fclose(f);
return 0;
}