// FILE HANDLING
// ACCESS FILE CONTENTS RANDOMLY
//fseek() - bring the cursor to desired position START||END||BETWEEN
//rewind() - set the cursor to initial position 0
//ftell() - print the cursor position (INTEGER VALUE OR DOUBLE VALUE)
//fgetpos() - write the contents in new position
//fsetpos() - override the fgetpos() function
#include stdio.h
int main()
{
// CREATE FILE POINTER
FILE *fp;
fpos_t position;
//CREATE A FILE USING fopen() FUNCTION
fp=fopen("tutorial_file.txt","w");
//ADD SOME CONTENTS TO FILE USING fputs() FUNCTION
fputs("Test Contents to File !!!!",fp);
//NOW USE FSEEK FUNCTION
//fseek(fp,0,SEEK_END); // IT WILL BRING THE POINTER TO END OF THE FILE CONTENT WITH 0 DISTANCE
// BRING THE CURSOR TO STARTING POSITION
// TO BRING THE CURSOR TO STARTING POSITION WE CAN ALSO USE REWIND FUNCTION
//fseek(fp,0,SEEK_SET);
//rewind(fp);
// BRING THE POINTER TO DESIRED POSITION
fseek(fp,0,SEEK_END); // NOW OUR CURSOR IS SET TO 5 TH POSITION
// NOW PRINT THE FILE POINTER POSITION
printf("%ld",ftell(fp));
// WRITE THE FILE CONTENT USING fgetpos() FUNCTION
fgetpos(fp,&position);
fputs("This is new file content using fgetpos !!!!!!",fp);
// OVERRIDE THE ABOVE FUNCTION USING fsetpos() FUNCTION
fsetpos(fp,&position);
fputs("This is overridden file content using fsetpos!!!!!!!",fp);
//CLOSE THE FILE USING fclose() function
fclose(fp);
return 0;
}
//RUN THE PROGRAM CTRL+F5