Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

The fseek() function

Syntax

int fseek(FILE *fp, long int offset, int origin);

This function is used to place the file position indicator at any specific point in the file.
Using this indicator file can be read from any specific point or file can be written from any specific point.

Example

#include<stdio.h>

int main()
{
    FILE *fp;
    char str1[50] = "Hello! Have a Nice Day.";
    char str2[50];
    fp=fopen("D:\\C Programming\\file8.txt","w"); // File opened in 
                                                   // write mode
    if(fp==NULL)
    {
        printf("\n Unable to open the FILE");
        return 1;
    }
    fputs(str1,fp);
    fclose(fp);  // File closed
	
    fp=fopen("D:\\C Programming\\file8.txt","r"); // File opened in 
                                                  // read mode
    fseek(fp,7,SEEK_SET);
	
    while(fgets(str2,20,fp)!=NULL)
    {
        puts(str2);
    }
    fclose(fp); // File closed
    return 0;
}

Inside file8.txt

C Programming Language fseek Function in File Handling Example

After fseek() execution

C Programming Language fseek Function in File Handling Example File Position Indicator

Output

C Programming Language fseek Function in File Handling Example Output
 

Leave a reply