C – fseek() function

fseek(): It is used to move the cursor position in the opened file, to perform read and write operations.

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

Parameters:

  • “offset” is a long type variable that represents the number of bytes to move.
    • Positive offset moves the cursor in forward direction
    • Negative offset moves the cursor in backward direction.
  • Origin comes with
    • SEEK_SET : Start of the file
    • SEEK_CURR : Current cursor position
    • SEEK_END : End of the file
  • On success, if specified location is present, it moves the cursor and returns 0
  • On failure, it returns -1.
#include<stdio.h>
int main()
{
            FILE* p;
            int ch;  
            p = fopen(“code.c”, “r”);
            if(p==NULL){
                        printf(“No such file to open \n”);
            }
            else{
                        fseek(p, 100, SEEK_SET);
                        while((ch=fgetc(p)) != -1){
                                    printf(“%c”, ch);           
                        }
                        fclose(p);
            }
            return 0;
}
Scroll to Top