Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C stdio fscanf() Function

❮ C stdio Library


Example

Read a string from a file:

FILE *fptr;

fptr = fopen("filename.txt", "r");

char output[50];
fscanf(fptr, "%49s", output);
printf("%s", output);

fclose(fptr);

Definition and Usage

The fscanf() function reads formatted data from a file and writes it into memory locations specified by the arguments, then moves the position indicator to the file position where it stopped reading.

The fscanf() function is defined in the <stdio.h> header file.

The format parameter is a string that describes the format of the data that is expected from the file. If the content of the file does not match the format then the function stops reading at the point where the first mismatch occurs.

Format specifiers

The format string can contain format specifiers which specify which parts of the data should be written to the arguments. Each format specifier corresponds to one of the additional arguments of the function.

The format specifiers have the form %[*][width][length]specifier. The components in [square brackets] are optional.

An explanation of each of the components:

  • * - Optional. When present, the format specifier does not correspond to an argument.
  • width - Optional. Specifies the maximum number of characters to read for this specifier.
  • length - Optional. A sequence of characters which changes the data type of the argument. It can be one of the following:
    • hh - Expect char* type for whole numbers.
    • h - Expect short* type for whole numbers.
    • l - Expect long int* type for whole numbers.
             Expect wchar_t* type for characters and strings.
             Expect double* type for floating point numbers.
    • ll - Expect long long int* type for whole numbers.
    • j - Expect intmax_t*or uintmax_t* type for whole numbers.
    • z - Expect size_t* type for whole numbers.
    • t - Expect ptrdiff_t* type for whole numbers.
    • L - Expect long double* type for whole numbers.
  • specifier - Required. A character or sequence which indicates how file data should be interpreted. The list of possible specifiers is shown in the table below.

List of specifiers

Character Specifier Description
i or u Integer Reads a sequence of digits and interprets them as an integer. If the sequence starts with "0x" then it expects hexadecimal digits (0-9 and A-F). If the sequence starts with "0" then it expects octal digits (0-7). The sequence may be preceded by a sign ("+" or "-").
d Decimal integer Reads a sequence of digits (0-9) and interprets them as an integer. The sequence may be preceded by a sign ("+" or "-").
o Octal integer Reads a sequence of digits (0-7) and interprets them as an octal integer. The sequence may be preceded by a sign ("+" or "-").
x Hexadecimal integer Reads a sequence of digits (0-9 and A-F) and interprets them as a hexadecimal integer. It may begin with "0x" The sequence may be preceded by a sign ("+" or "-").
f, e, g or a Floating point number Reads a valid sequence of characters and interprets them as a floating point number. A valid sequence has at least one digit, it can be preceded by a sign ("+" or "-") and it can be followed by a decimal point and decimal digits. Scientific notation (a number followed by "e" or "E" and some digits) can also be used.
c Character Reads a character from the file. If a width is specified then it reads that number of characters.
s String Reads all of the characters up to the next whitespace (space, tab, line break) in the file. The value written to the argument will have an additional \0 null terminating character appended to it.
p Pointer Reads a sequence of characters which represent a pointer address.
n No input Nothing is read from the file. The number of characters that have been read up to this point is written into the argument. The argument must be a pointer to an integer.
% Percent symbol Reads one character from the file expecting a "%" symbol. This specifier is not associated with an argument.
[characters] Character set Reads one character which matches one of the characters specified in characters.
[^characters] Excluded character set Reads one character which is not in the set of characters specified in characters.

See More Examples below for examples of how to use format specifiers.


Syntax

fscanf(FILE * fptr, const char * format, arg1, arg2...);

Parameter Values

Parameter Description
fptr Required. A file pointer, usually created by the fopen() function.
format Required. A string representing the format of the data expected from the file.
arg1, arg2... Optional. Any number of additional arguments which are pointers to memory where values can be written.

Technical Details

Returns: An int value representing the number of arguments that were written to. It returns the constant EOF if an error occurred.

More Examples

Example

Extract numbers from a file containing the sequence "1 + 2 = 3":

FILE *fptr;
fptr = fopen("filename.txt", "r");

int a, b, c;
fscanf(fptr, "%i + %i = %i", &a, &b, &c);
printf("a = %d \n", a);
printf("b = %d \n", b);
printf("c = %d \n", c);

fclose(fptr);

Example

Read a hexadecimal number from a file and output its value in decimal:

FILE *fptr;
fptr = fopen("filename.txt", "r");

int num;
fscanf(fptr, "%x", &num);
printf("%d", num);

fclose(fptr);

Example

Search for either "x", "y" or "z" in the next character of the file:

FILE *fptr;
fptr = fopen("filename.txt", "r");

char c;
int found = fscanf(fptr, " %[xyz]", &c);
if (found > 0) {
  printf("Found %c", c);
} else {
  printf("Character not found");
}

fclose(fptr);

❮ C stdio Library

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.