C library functions -fscanf()

C Standard Library - <stdio.h>

Description

C Library Functionsint fscanf(FILE *stream, const char *format, ...)Reads formatted input from the stream.

Declaration

Below is the declaration of the fscanf() function.

int fscanf(FILE *stream, const char *format, ...)

Parameter

  • stream-- This is a pointer to a FILE object that identifies the stream.
  • format-- This is a C string that contains one or more of the following items:whitespace characters, non-whitespace charactersandformat specifiers。
    The format specifier takes the form[=%[*][width][modifiers]type=], as explained in detail below:
ParameterDescription
*This is an optional asterisk, indicating that the data is read from the stream but can be ignored, i.e., it is not stored in the corresponding argument.
widthThis specifies the maximum number of characters to be read in the current read operation.
modifiersSpecifies a different size for the data pointed to by the corresponding additional argument, other than integer (for d, i, and n), unsigned integer (for o, u, and x), or floating-point (for e, f, and g): h: short integer (for d, i, and n), or unsigned short integer (for o, u, and x) l: long integer (for d, i, and n), or unsigned long integer (for o, u, and x), or double (for e, f, and g) L: long double (for e, f, and g)
typeA character that specifies the type of data to be read and the way data is read. See the next table for details.

fscanf type specifiers:

TypeQualifying inputargument type
cSingle character: reads the next character. If a width other than 1 is specified, the function reads width characters and stores them in successive positions of the array passed via the argument. No null character is appended at the end.char *
dDecimal integer: the + or - sign before the number is optional.int *
e,E,f,g,GFloating-point number: contains a decimal point, an optional leading sign + or -, an optional trailing character e or E, and a decimal digit. Two valid examples are -732.103 and 7.12e4float *
oOctal integer.int *
sString. This reads successive characters until a whitespace character is encountered (whitespace characters may be blanks, newlines, and tabs).char *
uUnsigned decimal integer.unsigned int *
x,XHexadecimal integer.int *
  • Additional arguments-- Depending on the format string, the function may expect a sequence of additional arguments, each containing a value to be inserted, replacing each % tag specified in the format parameter. The number of arguments should be the same as the number of % tags.

Return Value

If successful, the function returns the number of successfully matched and assigned items. If the end of the file is reached or a read error occurs, it returns EOF.

Example

The following example demonstrates the usage of the fscanf() function.

#include <stdio.h>
#include <stdlib.h>


int main()
{
   char str1[10], str2[10], str3[10];
   int year;
   FILE * fp;

   fp = fopen ("file.txt", "w+");
   fputs("We are in 2014", fp);
   
   rewind(fp);
   fscanf(fp, "%s %s %s %d", str1, str2, str3, &year);
   
   printf("Read String1 |%s|\n", str1 );
   printf("Read String2 |%s|\n", str2 );
   printf("Read String3 |%s|\n", str3 );
   printf("Read Integer |%d|\n", year );

   fclose(fp);
   
   return(0);
}

Let us compile and run the above program, which will produce the following result:

Read String1 |We|
Read String2 |are|
Read String3 |in|
Read Integer |2014|

C Standard Library - <stdio.h>

other extensions