Read Input From a File Using Scanf
C library function - fscanf()
Clarification
The C library role int fscanf(FILE *stream, const char *format, ...) reads formatted input from a stream.
Annunciation
Following is the declaration for fscanf() function.
int fscanf(FILE *stream, const char *format, ...)
Parameters
-
stream − This is the pointer to a FILE object that identifies the stream.
-
format − This is the C string that contains i or more of the post-obit items − Whitespace character, Not-whitespace character and Format specifiers. A format specifier will be every bit [=%[*][width][modifiers]type=], which is explained below −
Sr.No. | Statement & Description |
---|---|
1 | * This is an optional starting asterisk indicates that the data is to be read from the stream simply ignored, i.east. it is not stored in the corresponding argument. |
2 | width This specifies the maximum number of characters to exist read in the electric current reading operation. |
iii | modifiers Specifies a size dissimilar from int (in the case of d, i and n), unsigned int (in the case of o, u and 10) or float (in the instance of e, f and one thousand) for the information pointed past the respective additional statement: h : curt int (for d, i and n), or unsigned short int (for o, u and ten) 50 : long int (for d, i and n), or unsigned long int (for o, u and ten), or double (for e, f and k) Fifty : long double (for due east, f and g) |
4 | type A graphic symbol specifying the type of data to be read and how it is expected to be read. Come across next table. |
fscanf type specifiers
type | Qualifying Input | Blazon of argument |
---|---|---|
c | Unmarried grapheme: Reads the next character. If a width different from i is specified, the office reads width characters and stores them in the successive locations of the array passed as statement. No aught character is appended at the end. | char * |
d | Decimal integer: Number optionally preceded with a + or - sign | int * |
due east, E, f, g, G | Floating bespeak: Decimal number containing a decimal signal, optionally preceded by a + or - sign and optionally followed by the e or E character and a decimal number. Ii examples of valid entries are -732.103 and 7.12e4 | float * |
o | Octal Integer: | int * |
due south | 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, 10 | Hexadecimal Integer | int * |
-
additional arguments − Depending on the format string, the role may expect a sequence of additional arguments, each containing 1 value to exist inserted instead of each %-tag specified in the format parameter (if any). There should be the same number of these arguments as the number of %-tags that look a value.
Return Value
This function returns the number of input items successfully matched and assigned, which tin can be fewer than provided for, or fifty-fifty cipher in the event of an early matching failure.
Case
The following example shows the usage of fscanf() part.
#include <stdio.h> #include <stdlib.h> int master () { char str1[x], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "due west+"); fputs("We are in 2012", fp); rewind(fp); fscanf(fp, "%due south %s %southward %d", str1, str2, str3, &year); printf("Read String1 |%south|\due north", str1 ); printf("Read String2 |%s|\n", str2 ); printf("Read String3 |%s|\north", str3 ); printf("Read Integer |%d|\north", year ); fclose(fp); render(0); }
Let usa compile and run the higher up program that will produce the following result −
Read String1 |We| Read String2 |are| Read String3 |in| Read Integer |2012|
stdio_h.htm
Useful Video Courses
Video
Video
Video
Video
Video
Video
mcclainthioseen37.blogspot.com
Source: https://www.tutorialspoint.com/c_standard_library/c_function_fscanf.htm
0 Response to "Read Input From a File Using Scanf"
Post a Comment