The C library int fscanf(FILE *stream, const char *format, ...) function reads formatted input from a stream. It is part of the Standard I/O library and is defined in the <stdio.h> header. This function allows for extracting and parsing data from a file according to a specified format.
SyntaxFollowing is the C library syntax of the fscanf() function −
int fscanf(FILE *stream, const char *format, ...);Parameters
This function uses various parameter to works on file handling &minus-
*
This is 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 corresponding argument.
2width
This specifies the maximum number of characters to be read in the current reading operation.
3modifiers
Specifies a size different from int (in the case of d, i and n), unsigned int (in the case of o, u and x) or float (in the case of e, f and g) for the data pointed by the corresponding additional argument: h : short int (for d, i and n), or unsigned short int (for o, u and x) l : long int (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g) L : long double (for e, f and g)
4type
A character specifying the type of data to be read and how it is expected to be read. See next table.
type Qualifying Input Type of argument c Single character: Reads the next character. If a width different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char * d Decimal integer: Number optionally preceded with a + or - sign int * e, E, f, g, G Floating point: Decimal number containing a decimal point, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 float * o Octal Integer: int * s String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). char * u Unsigned decimal integer. unsigned int * x, X Hexadecimal Integer int * Return valueThe fscanf function returns an integer value representing the number of input items successfully matched and assigned. If the input ends before the first matching failure or if an error occurs, EOF (End Of File) is returned.
Example 1: Reading Integer and StringThe below code reads an integer and a string from the file and prints them.
Following is the illustration of the C library fscanf() function.
#include <stdio.h> #include <stdlib.h> int main() { const char *filename = "example1.txt"; FILE *file = fopen(filename, "r"); // If the file does not exist, create and write initial data to it if (file == NULL) { file = fopen(filename, "w"); if (file == NULL) { perror("Error creating file"); return 1; } fprintf(file, "123 HelloWorld\n"); fclose(file); file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return 1; } } int number; char word[50]; if (fscanf(file, "%d %49s", &number, word) == 2) { printf("Number: %d, Word: %s\n", number, word); } else { printf("Failed to read data from file.\n"); } fclose(file); return 0; }Output
The above code produces following result−
Number: 123, Word: HelloWorldExample 2: Reading Multiple Floating Point Numbers
The below code reads three floating-point numbers from the file and prints them.
#include <stdio.h> #include <stdlib.h> int main() { const char *filename = "example2.txt"; FILE *file = fopen(filename, "r"); // If the file does not exist, create and write initial data to it if (file == NULL) { file = fopen(filename, "w"); if (file == NULL) { perror("Error creating file"); return 1; } fprintf(file, "3.14 2.718 1.618\n"); fclose(file); file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return 1; } } float num1, num2, num3; if (fscanf(file, "%f %f %f", &num1, &num2, &num3) == 3) { if (fscanf(file, "%f %f %f", &num1, &num2, &num3) == 3) { printf("Numbers: %.2f, %.2f, %.2f\n", num1, num2, num3); } else { printf("Failed to read data from file.\n"); } fclose(file); return 0; }Output
After execution of above code, we get the following result
Numbers: 3.14, 2.72, 1.62Example 3: Reading Date from File
The below code reads a date in DD/MM/YYYY format and prints it in DD-MM-YYYY format.
#include <stdio.h> #include <stdlib.h> int main() { const char *filename = "example3.txt"; FILE *file = fopen(filename, "r"); // If the file does not exist, create and write initial data to it if (file == NULL) { file = fopen(filename, "w"); if (file == NULL) { perror("Error creating file"); return 1; } fprintf(file, "23/05/2024\n"); fclose(file); file = fopen(filename, "r"); if (file == NULL) { perror("Error opening file"); return 1; } } int day, month, year; if (fscanf(file, "%d/%d/%d", &day, &month, &year) == 3) { printf("Date: %02d-%02d-%04d\n", day, month, year); } else { printf("Failed to read data from file.\n"); } fclose(file); return 0; }Output
The output of the above code is as follows −
Date: 23-05-2024
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4