YouTip LogoYouTip

C Function Fscanf

# C Library Function - fscanf() [![Image 3: C Standard Library - ](#) C Standard Library - ](#) ## Description The C library function **int fscanf(FILE *stream, const char *format, ...)** reads formatted input from a stream. ## Declaration Here is the declaration for the fscanf() function. int fscanf(FILE *stream, const char *format, ...) ## Parameters * **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 characters,_ and _format specifiers_. The format specifier is in the form **[=%[*]type=]**, which is explained in detail below: | Parameter | Description | | --- | --- | | * | This is an optional asterisk indicating that the data is to be read from the stream but ignored, i.e., it is not stored in the corresponding argument. | | width | This specifies the maximum number of characters to be read in the current reading operation. | | modifiers | Specifies a size different from int (for d, i, and n), unsigned int (for o, u, and x), or float (for e, f, and g) for the data pointed to 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) | | type | A character specifying the type of data to be read and how it is read. See the next table for details. | **fscanf Type Specifiers:** | Type | Qualifying Input | Parameter Type | | --- | --- | --- | | 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 array passed as argument. No null character is appended at the end. | char * | | d | Decimal integer: The number is optionally preceded by a + or - sign. | int * | | e,E,f,g,G | Floating point: Includes a decimal point, an optional leading + or - sign, an optional trailing e or E, and a decimal digit. Two valid examples are -732.103 and 7.12e4 | float * | | o | Octal integer. | int * | | s | String. This reads consecutive characters until a whitespace character is encountered (whitespace characters can be space, newline, and tab). | char * | | u | Unsigned decimal integer. | unsigned int * | | x,X | Hexadecimal 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 in place of each % tag in the format string. The number of arguments should match the number of % tags. ## Return Value If successful, the function returns the number of items successfully matched and assigned. If a reading error occurs or the end of file is reached, EOF is returned. ## Example The following example demonstrates the usage of the fscanf() function. #include #include int main(){ char str1, str2, str3; 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| [![Image 4: C Standard Library - ](#) C Standard Library - ](#)
← C Function SscanfSets Spop β†’