Function fscanf () in C
The int fscanf function (FILE * stream, const char * format, .) in Library C reads the input formatted from a Stream.
Declare the function fscanf () in C
Below is the declaration for fscanf () function in C:
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 string that contains one or more of the following: Whitespace characters, Non-whitespace and Format specifier characters. A format specifier will be the form [=% [*] [width] [modifiers] type =] , explained as follows:
ParametersDescription * This is a starting asterisk (optional) to indicate that the data is read from the Stream but ignored, for example, it is not stored in the corresponding parameter width Specifies the number of characters maximum read in current readings modifiers Define a different size from int (in the case of d, I and n), unsigned int (in the case of o, u and x) or float (in case e, f and g) for data pointed by the corresponding additional parameter: h for int short (for d, i and n), or unsigned short int (for o, u and x); l for int long (for d, i and n), or unsigned long int (for o, u and x), or double (for e, f and g); L for long double (for e, f and g) type A character specifying the type of data to be read and how it is expected to be read. You follow the next tableType specifier for fscanf function
TypeInput Shock type Single character: Read the next character. If a width other than 1 is specified, this function reads the width of characters and stores them in consecutive array positions that have been passed as parameters. No null characters are appended to the end of char * d Decimal integer numbers: This number is arbitrarily preceded by a + sign or - int * e, E, f, g, G floating point numbers: The decimal number contains a decimal point, arbitrarily preceded by a + or - sign and followed arbitrarily by the letter e or E and a decimal number. Two valid examples are -732.103 and 7.12e4 float * o Octal integer int * s Character string. It will read consecutive characters until a whitespace is found (maybe blank, newline) and tab) char * u Decimal integers unsigned int * x, X Hexadecimal integers segment int *Additional parameters - Depending on the format format string, this function may have an additional parameter array, each containing a value to be inserted instead of each% -tag specified in the format parameter. , if. This number of parameters should be the same number as% -tags that expect a value.
Returns the value
This function returns the number of entries connected and successfully assigned or 0 if the connection failed.
For example
The following program C illustrates the usage of the fscanf () function in C:
#include #include int main () { char str1 [ 10 ], str2 [ 10 ], str3 [ 10 ]; int year ; FILE * fp ; fp = fopen ( "baitapc.txt" , "w+" ); fputs ( "Toi sinh nam 2016" , fp ); rewind ( fp ); fscanf ( fp , "%s %s %s %d" , str1 , str2 , str3 , & year ); printf ( "Doc chuoi 1: |%s|n" , str1 ); printf ( "Doc chuoi 2: |%s|n" , str2 ); printf ( "Doc chuoi 3: |%s|n" , str3 ); printf ( "Doc so nguyen: |%d|n" , year ); fclose ( fp ); return ( 0 ); }
Compiling and running the above C program will result:
According to Tutorialspoint
Previous lesson: The function vsprintf () in C
Next lesson: Function scanf () in C
You should read it
May be interested
- Hex () function in Pythonthe hex () function is one of python's built-in functions, used to convert an integer into the corresponding hexadecimal form.
- ABS function in SQL Serverthis article will show you in detail how to use the abs () handling function in sql server with specific syntax and examples to better visualize and capture functions.
- The function id () in Pythonthe built-in function id () in python returns a single integer value that identifies an object.
- DATEPART function in SQL Serverthe datepart function in sql server returns a time value of the input argument, which can be day, month, year, quarter, hour, minute, second, millisecond ... the return value is an integer type (int)
- How to use the SUM function to calculate totals in Excelsum is a popular and very useful excel function, and is also a basic arithmetic function. as its name suggests, the sum function is used to calculate totals in excel. and the parameters can be single parameters or ranges of cells. in this article, tipsmake.com will guide you to use the sum function to calculate the sum in excel, the common errors when calculating sum by sum and how to fix it.
- LEN function in SQL Serverthe len function in sql server returns the length of the specified string. it is important that the len function does not include whitespace characters at the end of the string when calculating length.
- ROUND function in SQL Serverthe article will give you detailed instructions on how to use the sql server round () function with the syntax and specific examples to make it easier to visualize and capture the function better.
- Max () function in Pythonpython's built-in max () function returns the largest element in an iterable or the largest of the passed parameters
- The function dir () in Pythonthe dir () function in python returns a list of valid properties of the object. quantrimang will learn more about this function content through the article. invites you to read the track.
- Bin () function in Pythonin python programming, the bin () function converts and returns the binary string of the given integer. so what is the syntax of bin () function, parameters and how to use bin ()? let's find out together.