C <stdio.h> - vfscanf() Function
The C <stdio.h> vfscanf() function reads data from the stream, interprets it according to parameter format and stores the results into the locations pointed by the elements in the variable argument list identified by vlist.
Internally, the function retrieves arguments from the list identified by vlist as if va_start was used on it, and thus the state of vlist is likely altered by the call.
In any case, vlist should have been initialized by va_start at some point before the call, and it is expected to be released by va_end at some point after the call.
Syntax
int vfscanf ( FILE * stream, const char * format, va_list vlist );
Parameters
stream |
Specify a pointer to a FILE object that specifies an input stream to read data from. | |||||||||||||||||||||||||||||||||||||||||||||||
format |
Specify the format string. The format string contains a sequence of characters that control how characters extracted from the stream are treated:
A format specifier for vfscanf() follows this prototype: %[*][width][length]specifier The following format specifiers are available:
Except for n, at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there. Additional format values can be placed between the % and the specifier (also known as sub-specifier), which are as follows:
| |||||||||||||||||||||||||||||||||||||||||||||||
vlist |
Specify a variable argument list containing the receiving arguments initialized with va_start. |
Return Value
Returns number of receiving arguments successfully assigned (which may be zero in case a matching failure occurred before the first receiving argument was assigned), or EOF if input failure occurs before the first receiving argument was assigned.
If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof() or ferror()). And, if either happens before any data could be successfully read, EOF is returned.
If a multibyte character encoding error occurs while interpreting wide characters, errno is set to EILSEQ.
Example: vfscanf() example
Lets assume that we have a file called employees.txt. This file contains following content:
John 25 3000 Marry 24 2750 Jo 27 2800 Kim 30 3100 Ramesh 28 3000
In the example below, the file is opened using fopen() function. Then the content of the file is parsed according to the specified format using vfscanf() function. After performing this operation, it is closed using fclose() function.
#include <stdio.h> #include <stdarg.h> void ScanFormatted ( FILE * stream, const char * format, ... ) { va_list args; va_start(args, format); vfscanf(stream, format, args); va_end(args); } int main (){ char name [80]; int age, salary; //open the file in read mode FILE *pFile = fopen("employees.txt", "r"); //reads data from the file until EOF is reached while (!feof(pFile)) { ScanFormatted(pFile, "%s %i %i", name, &age, &salary); printf("%s is %i years old and earns %i dollars.\n", name, age, salary); } //closing the file fclose(pFile); return 0; }
The output of the above code will be:
John is 25 years old and earns 3000 dollars. Marry is 24 years old and earns 2750 dollars. Jo is 27 years old and earns 2800 dollars. Kim is 30 years old and earns 3100 dollars. Ramesh is 28 years old and earns 3000 dollars.
Length sub-specifier
Specifiers | |||||||
---|---|---|---|---|---|---|---|
length | d i | u o x X | f F e E g G a A | c s [ ] [^] | p | n | |
(none) | int* | unsigned int* | float* | char* | void** | int* | |
hh | signed char* | unsigned char* | signed char* | ||||
h | short int* | unsigned short int* | short int* | ||||
l | long int* | unsigned long int* | double* | wchar_t* | long int* | ||
ll | long long int* | unsigned long long int* | long long int* | ||||
j | intmax_t* | uintmax_t* | intmax_t* | ||||
z | size_t* | size_t* | size_t* | ||||
t | ptrdiff_t* | ptrdiff_t* | ptrdiff_t* | ||||
L | long double* |
See <cinttypes> for the specifiers for extended types.
❮ C <stdio.h> Library