C File I/O Operations : fseek()

The fseek() function is used to move file pointer associated with a given file to a specific position. For example if we have many records inside a file and need to access a record at a specific position, so at that situation we can use fseek() function. Now as the name suggests, fseek() seeks the cursor to the given record or location in the file.

The syntax of fseek() function is :
 fseek(FILE *sp, long int numbytes, int origin);
Where :
  • fp is a file pointer, which pointed to the opened file.
  • numbytes is the number of bytes from origin that will become the new current position.
  • origin specifies one of the following macros :

Macro Name Meaning
SEEK_SET Beginning of file
SEEK_CUR Current position
SEEK_END End of file

Therefore to seek numbytes from the start of the file, origin should be SEEK_SET. To seek from the current position, use SEEK_CUR and to seek from the end of the file use SEEK_END.

The fseek() function returns 0 when successful and a nonzero value if an error occurs.

Now lets see C implementation of fseek() function. In our previous tutorial about fwrite() function we create a file books.dat and stored some information on it and after that read the info from  the file with fread() function. Below code will create the file books.dat.
#include <stdio.h>
#include <string.h>

struct bookDB {
  char name[20];
  char author[20];
  int price;
};

int main() {
  struct bookDB b1;
  FILE *fp;
  fp = fopen("books.dat", "wb");

  strncpy(b1.name, "C Programming", 20);
  strncpy(b1.author, "Dennis Ritchie", 20);
  b1.price = 250;
  fwrite(&b1, sizeof(b1), 1, fp);

  strncpy(b1.name, "C Sockets", 20);
  strncpy(b1.author, "John Forgers", 20);
  b1.price = 230;
  fwrite(&b1, sizeof(b1), 1, fp);

  strncpy(b1.name, "C Complete Ref.", 20);
  strncpy(b1.author, "Herbert Schildt", 20);
  b1.price = 230;
  fwrite(&b1, sizeof(b1), 1, fp);

  fclose(fp);
  printf("Data Successfully written into books.dat.\n");
  return 0;
}
Output :

Data Successfully written into books.dat.

Now as we know that there are three entries of book details on the file books.dat.
 C Programming
 Dennis Ritchie
 250

 C Sockets
 John Forgers
 230

 C Complete Ref.
 Herbert Schildt
 230
The C code below prints the 2nd book details :
#include <stdio.h>
#include <string.h>

struct bookDB {
  char name[20];
  char author[20];
  int price;
};

int main() {
  struct bookDB bd;
  FILE *fp;
  fp = fopen("books.dat", "rb");
  if(fp == NULL) {
    printf("Error : could not nopn file.\n");
    return -1;
  }

  printf("Book Details :\n\n");
  fseek(fp, sizeof(bd), SEEK_SET);
  fread(&bd,sizeof(bd), 1, fp);
  printf("Name : %s\n", bd.name);
  printf("Author : %s\n", bd.author);
  printf("Price : %d\n\n", bd.price);

  fclose(fp);
  return 0;
}
Output :

Book Details :

Name : C Sockets
Author : John Forgers
Price : 230

At above code the fseek() statement would be :
 fseek(fp, sizeof(bd), SEEK_SET);
where third parameter SEEK_SET will set the pointer to beginning of the file and second argument move the pointer position with sizeof(bd) [size of structure bookDB], which makes the pointer to point the second book details.

Similarly the below C code will print the last book detail.
#include <stdio.h>

struct bookDB {
  char name[20];
  char author[20];
  int price;
};

int main() {
  struct bookDB bd;
  FILE *fp;
  fp = fopen("books.dat", "rb");
  if(fp == NULL) {
    printf("Error : could not nopn file.\n");
    return -1;
  }

  printf("Book Details :\n\n");
  fseek(fp, -sizeof(bd), SEEK_END);
  fread(&bd,sizeof(bd), 1, fp);
  printf("Name : %s\n", bd.name);
  printf("Author : %s\n", bd.author);
  printf("Price : %d\n\n", bd.price);

  fclose(fp);
  return 0;
}
Output :

Book Details :

Name : C Complete Ref.
Author : Herbert Schildt
Price : 230

At above code the fseek() statement would be :
 fseek(fp, -sizeof(bd), SEEK_END);
Where SEEK_END will set the pointer to end of the file and -sizeof(bd) will move the pointer to upside from the current position.

Topics :