fscanf : <stdio.h> library function

The function int fscanf(FILE *stream, const char *format, ...); reads formatted data from the given stream and stores them in the variables pointed by the additional arguments. Additional arguments must point to variables of the same type as specified in the format.

Format is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.

The syntax of the format specifier is as follows: [*][width][length]type

Below is the description of each component of format specifier.

Component Description
* This is an optional starting asterisk indicates that the data to be read from the stream but will be ignored. It won't get stored in any memory location.
width This specifies the maximum number of characters to be read from stream for this read operation.
length Specifies a size different from the data pointed by the corresponding additional argument.
type A character specifying the type of data to be read from stream and in what format it is expected to be read.

fscanf type specifiers

Specifier Description
d Decimal integer. Any number of decimal digits (0-9), optionally preceded by a '+' or '-' sign.
u Unsigned decimal integer.
o Octal integer. Any number of octal digits (0-7), optionally preceded by a '+' or '-' sign.
x Hexadecimal integer. Any number of hexadecimal digits (0-9, a-f, A-F), and optionally preceded by a '+' or '-' sign.
f, e or g Floating point number. A decimal number, may containing a decimal point, optionally preceded by a '+' or '-' sign and optionally followed by the e or E character and a decimal integer.
c Character Reads a character from stream. If a width greater than one is specified, the function reads width characters and stores them in the successive locations of the array passed as argument.
s Sequence of Characters It will keep on reading characters from stream untill it finds a white space character(whitespace characters are considered to be blank, newline and tab). A terminating null character ('\0') is added at the end of the stored characters.
[characters] It will read only those characters specified between the brackets.
[^characters] It will read only those characters which is not specified between the brackets.

Function prototype of fscanf

int fscanf(FILE *stream, const char *format, ...);
  • stream : This is the pointer to a FILE object that identifies the stream.
  • format : This is a null terminated string that contains Whitespace character, Non-whitespace character and Format specifiers.
  • additional arguments : As per the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the data read from stream to be stored.

Return value of fscanf

On success, this function returns the total number of objects successfully read, it may or may not be same as the expected number of items specified in format string.

C program to show the use of fscanf function

The following program shows the use of fscanf function to read formatted data from a stream.

#include <stdio.h>

int main()
{
   char string[50];
   int val;
   float fval;
   FILE *file;

   file = fopen ("textFile.txt", "w+");
   fprintf(file, "%d %f %s", 5, 5.5, "TechCrashCourse.com");
   rewind(file);

   fscanf(file, "%d %f %s", &val, &fval, string);
   
   printf("Integer : %d\n", val);
   printf("Floating point number : %f\n", fval);
   printf("String : %s\n", string);

   fclose(file);
   return(0);
}

Program Output
Integer : 5
Floating point number : 5.500000
String : TechCrashCourse.com