function
<cwchar>
fwscanf
int fwscanf (FILE* stream, const wchar_t* format, ...);
Read formatted data from stream
Reads data from the stream and stores them according to the C wide string format into the locations pointed by the additional arguments.
The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.
The external representation of wide characters in files are multibyte characters: These are translated as if mbrtowc was called (using the stream's internal mbstate_t object).
This is the wide character equivalent of fscanf (<cstdio>).
Parameters
- stream
- Pointer to a FILE object that identifies the input stream to read data from.
The stream shall not have an orientation yet, or be wide-oriented (the first i/o operation on a stream determines whether it is byte- or wide- oriented, see fwide).
- format
- C wide string that contains a format string that follows the same specifications as format in scanf (see scanf for details).
Notice though, that all format specifiers have the same meaning as in scanf; therefore, %lc shall be used to read a wide character (and not %c), as well as %ls shall be used for wide strings (and not %s).
- ... (additional arguments)
- Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type.
There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.
Return Value
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
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 an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
/* fwscanf example */
#include <stdio.h>
#include <wchar.h>
int main ()
{
wchar_t str [80];
float f;
FILE * pFile;
pFile = fopen ("myfile.txt","w+");
fwprintf (pFile, L"%f %ls", 3.1416, L"PI");
rewind (pFile);
fwscanf (pFile, L"%f", &f);
fwscanf (pFile, L"%ls", str);
fclose (pFile);
wprintf (L"I have read: %f and %ls \n",f,str);
return 0;
}
|
This sample code creates a file called myfile.txt and writes a float number and a string to it. Then, the stream is rewinded and both values are read with fscanf. It finally produces an output similar to:
I have read: 3.141600 and PI
|
See also
- wscanf
- Read formatted data from stdin (function
)
- fwprintf
- Write formatted data to stream (function
)
- fread
- Read block of data from stream (function
)
- fgetws
- Get wide string from stream (function
)