C File I/O Operations : fscanf()

The fscanf()function is used to read formatted data from a file. The fscanf() function is similar to scanf() function except the first argument which is a file pointer that specifies the file to be read. The syntax of fscanf() function is as follows :
 fscanf(FILE *fp, const char *format, var-list...);
Where :
  • fp is file pointer to identify input file.
  • format is the C format specifier, which contains one or more of the following items − Whitespace character, Non-whitespace character and Format specifiers. A format specifier for fscanf follows this prototype:
  • var-list is nothing but the list of variables where the extracted data will be stored.
 %[*][width][length]specifier 
Where the specifier character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:

Specifier Used for
i, u Integer
d Decimal integer
o Octal integer
x Hexadecimal integer
f, e, g,a Floating point number
c Character
s String of characters
p Pointer address

And also :

% : A % followed by another % matches a single %.

* :   An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).

width : Specifies the maximum number of characters to be read in the current reading operation (optional).

length : One of hh, h, l, ll, j, z, t, L (optional). This alters the expected type of the storage pointed by the corresponding argument (see below).

Now lets see some C example programs of it.

Example 1 :
#include <stdio.h>

int main() {
  FILE *fp;
  char str1[30], str2[30];
  int num;

  // writing data to the file
  fp=fopen("file.txt", "w");
  fprintf(fp, "%s %d %s", "Bruce", 45, "Male");
  fclose(fp);

  //read contents of file
  printf("Printing contents of file.txt :\n");
  fp = fopen("file.txt", "r");
  fscanf(fp, "%s %d %s", str1, &num, str2);
  printf("Name : %s\n", str1);
  printf("Age : %d\n", num);
  printf("Gender : %s\n", str2);
  
  fclose(fp);

  return 0;
}
Output :

Printing contents of file.txt :
Name : Bruce
Age : 45
Gender : Male

Example 2 :
#include <stdio.h>

int main() {
  FILE *fp;
  char str1[30], str2[30];
  int num, i;

  // writing data to the file
  fp=fopen("file.txt", "w");
  printf("Enter Your Employee Details\n");
  for(i=0;i<3;i++) {
    printf("%d Employee :\n", i+1);
    printf("Name : ");
    scanf("%s", str1);
    printf("Age : ");
    scanf("%d", &num);
    printf("Gender : ");
    scanf("%s", str2);  
    fprintf(fp, "%s %d %s\n", str1, num, str2);
  }
  fclose(fp);

  //read contents of file
  printf("\nPrinting contents of file.txt :\n");
  fp = fopen("file.txt", "r");
  for(i=0;i<3;i++) {
    printf("%d Epmployee [ ", i+1);
    fscanf(fp, "%s %d %s", str1, &num, str2);
    printf("Name : %s | ", str1);
    printf("Age : %d | ", num);
    printf("Gender : %s ]\n", str2);
  }
  fclose(fp);

  return 0;
}
Output :

Enter Your Employee Details
1 Employee :
Name : John
Age : 25
Gender : Male
2 Employee :
Name : Marry
Age : 21
Gender : Female
3 Employee :
Name : Dinesh
Age : 45
Gender : Male

Printing contents of file.txt :
1 Epmployee [ Name : John | Age : 25 | Gender : Male ]
2 Epmployee [ Name : Marry | Age : 21 | Gender : Female ]
3 Epmployee [ Name : Dinesh | Age : 45 | Gender : Male ]

Topics :