w3resource

C fscanf() function

C library function - fscanf()

The fscanf() function is used to read data from the current position of the specified stream into the locations that are given by the entries in argument-list, if any. Each entry in argument-list must be a pointer to a variable with a type that corresponds to a type specifier in format-string.

Syntax:

int fscanf (FILE *stream, const char *format-string, argument-list);

Parameters:

Name Description
stream Identifies an address for a file descriptor, which is an area of memory associated with an input or output stream.
format The format argument is a string containing C language conversion specifications. The conversion specification specifies the notation, alignment, significant digits, field width, and other aspects of the output format. To represent non-printing characters, such as newlines and tabs, the format string may contain escape characters. Below are the details.
additional arguments Depending on the format string, the function may require additional arguments.

The format is a character string, beginning and ending in its initial shift state, if any, composed of zero or more directives. Each directive is composed of one of the following: one or more white-space characters ( <space>, <tab>, <newline>, <vertical-tab>, or <form-feed>); an ordinary character; or a conversion specification. Each conversion specification is introduced by the character '%' or the character sequence "%n$", after which the following appear in sequence:

  • An optional assignment-suppressing character '*'.
  • An optional non-zero decimal integer that specifies the maximum field width.
  • An optional assignment-allocation character 'm'.
  • An option length modifier that specifies the size of the receiving object.
  • A conversion specifier character that specifies the type of conversion to be applied. The valid conversion specifiers are described below.

A format specifier for fscanf() follows this prototype:

%[*][width][length]specifier

The following conversion specifiers are valid:

Character Type of input expected
d Matches an optionally signed decimal integer.
i Matches an optionally signed integer.
o Matches an optionally signed octal integer.
u Matches an optionally signed decimal integer.
x Matches an optionally signed hexadecimal integer,
a, e, f, g Matches an optionally signed floating-point number, infinity, or NaN
s Matches a sequence of bytes that are not white-space characters.
[ Matches a non-empty sequence of bytes from a set of expected bytes (the scanset).
c Matches a sequence of bytes of the number specified by the field width (1 if no field width is present in the conversion specification).
p Matches an implementation-defined set of sequences.
n No input is consumed. The application shall ensure that the corresponding argument is a pointer to the integer into which shall be written the number of bytes read from the input so far by this call to the fscanf() functions. Execution of a %n conversion specification shall not increment the assignment count returned at the completion of execution of the function. No argument shall be converted, but one shall be consumed.
C Equivalent to lc.
S Equivalent to ls.
% Matches a single '%' character; no conversion or assignment occurs. The complete conversion specification shall be %%.

Following table shows the length modifiers and their meanings:

Length modifiers Description
hh Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to signed char or unsigned char.
h Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to short or unsigned short.
l Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long or unsigned long; that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to double.
ll Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to long long or unsigned long long.
j Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to intmax_t or uintmax_t.
z Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to size_t or the corresponding signed integer type.
t Specifies that a following d, i, o, u, x, X, or n conversion specifier applies to an argument with type pointer to ptrdiff_t or the corresponding unsigned type.
L Specifies that a following a, A, e, E, f, F, g, or G conversion specifier applies to an argument with type pointer to long double.

Return value

  • Upon successful completion, these functions shall return the number of successfully matched and assigned input items;
  • this number can be zero in the event of an early matching failure.

Example: fscanf() function

The following example creates a file called test.txt and writes a string, an integer and a float number. The stream is then rewinded and both values are read using fscanf. Finally, it produces an output:

#include <stdio.h>
int main ()
{
  char str1 [12], str2 [14];
  float f;
  int g;
  FILE * fp;
  fp = fopen ("test.txt","w+");
  fprintf(fp, "%s  %f", "Price:", 234.32);
  fprintf(fp, "\n%s  %d", "Pieces:", 120);
  rewind (fp);
  fscanf (fp, "%s", str1);
  fscanf (fp, "%f", &f);

  fscanf (fp, "%s", str2);
  fscanf (fp, "%d", &g);  
  fclose (fp);
  printf ("%s %2f \n",str1,f);
  printf ("%s %d \n",str2,g);
  return 0;
}

Output:

Price: 234.320007
Pieces: 120

C Programming Code Editor:

Previous C Programming: C vsprintf()
Next C Programming: C scanf()



Follow us on Facebook and Twitter for latest update.