fscanf() Function in the C++
In the C++ programming language, the fscanf() method can be used to read data from a file stream.
Syntax:
The syntax for the fscanf() function in the C++ programming language is as follows:
int fscanf( FILE* File_Name, const char* Format_Control, ... );
The fscanf() function scans the data from a file stream and saves it in the appropriate variables. It is declared in the header file, "<cstdio>."
Parameters for fscanf() function
File_Name: It is an input file stream from which data is read.
Format_Control: It is a null-terminated "char" (character) string pointer that describes how to read input data. It is made up of format specifiers beginning with "%."
The "Format_Control" string is made up of the following components:
1. Non-whitespace Characters: Except "%," each consumes one unique "char" (character) from the input file stream. If the following character in the stream does not match equality, the function may be able to fail.
2. Whitespace Character: All whitespace characters that follow one another are considered a single whitespace character. Furthermore, "\n," "\t," and " " are all considered to be the same.
3. Conversion Specifications:
It is formatted as follows:
- The first "%" character indicates the beginning.
- The character "*" is known as an assignment-suppressing character (optional).
- If such a character is included, the result of the fscanf() function is not assigned to any accepting argument.
- The maximum field width is specified by a positive integer value (optional). It defines the maximum number of characters that the fscanf() function may use while performing the conversion given by the conversion specifications.
- The receiving argument's size is specified using a length modifier (optional).
Some Format Specifiers:
- “%” - It corresponds to the literal "%."
- “c” (Characters) - It can match a single character or several characters. If width is specified, then the width characters must match exactly.
- “s” (String of characters) - Consecutive non-whitespace characters are matched. If width is specified, it must match the exact width of the characters or until the first whitespace is encountered.
- “[character set]” (Scan set) - Any combination of the characters between the brackets. In some library implementations, a dash (-) that is not the initial character may cause non-portable behaviour.
- “[^ character set]” (Negated scan set) - Any combination of characters, none of which are indicated as characters between the brackets.
- “d” (Decimal integer) - Any combination of decimal numbers (0–9), optionally followed by a plus or minus sign (+ or –).
- “i” (Integers) - This corresponds to an integer.
- “u” (Integers) - It corresponds to an “unsigned” decimal integer.
- “o” (Octal integer) - Any number of unsigned octal numbers (0–7) followed by a sign (+ or -).
- “x” (Hexadecimal integer) - Any number of unsigned hexadecimal numbers (0-9, A-F), optionally followed by 0x, and all optionally followed by a sign (+ or -).
- “a,” “f,” “e,” “g” (Float point integers) - It corresponds to a float-point integer.
- “n” (Count) - There is no user input.
The number of characters read from "File_Name" so far is saved in the location specified. - “p” (Pointer) - It matches a character sequence defined by the implementation that defines a pointer.
As a result, the general syntax of the format specifier is as follows:
%[*][width][length]specifier
- Another argument for receiving the data is that it appears in the order specified by the format specifier.
“fscanf()” Function Return Value
- If successful, then the fscanf() method returns the number of successfully allocated receiving parameters.
- It returns 0 if a matching failure happens before the initial receiving argument is assigned.
- If the input fails before the initial receiving parameter is assigned, the error code EOF is returned.
Example
#include <cstdio>
int main (){
FILE *file_name;
char name[100];
int age;
file_name = fopen("example.txt","w");
fprintf(file_name, "%s %d", "Krishna", 18);
fclose(file_name);
file_name = fopen("example.txt","r");
fscanf(file_name, "%s %d", name, &age);
fclose(file_name);
printf("Good day, %s. You have %d years of age.\n", name, age);
return 0;
}
Output:

“fscanf()” Function Compatibility
Further specifiers and sub-specifiers may be supported by specific library implementations.
Those mentioned here are supported by the most recent C and C++ programming standards (both published in 2011), but their lengths are "hh," "ll," and so on, which were introduced by the C'99 version (only necessary for C++ system implementation since C++11) and could not be supported by libraries that attach to older standards.