C <wchar.h> - swscanf() Function
The C <wchar.h> swscanf() function reads data from the wide string buffer, interprets it according to parameter format and stores the results 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.
Syntax
int swscanf ( const wchar_t* buffer, const wchar_t* format, ...);
Parameters
buffer |
Specify the pointer to a null-terminated wide string to read from. | |||||||||||||||||||||||||||||||||||||||||||||||
format |
Specify the C wide string that contains a format string. The format string contains a sequence of characters that control how characters extracted from the stream are treated:
A format specifier for wscanf() 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:
| |||||||||||||||||||||||||||||||||||||||||||||||
... (additional arguments) |
Depending on the format string, a sequence of additional arguments should be passed in the function, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type. Number of arguments should be at least equal to the number of format specifiers in the format string. Additional arguments will be ignored by this function. |
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 an encoding error occurs while interpreting wide characters, errno is set to EILSEQ.
Example: swscanf() example
The example below shows the usage of swscanf() function.
#include <wchar.h> int main (){ wchar_t mystr [] = L"John 25 3000"; wchar_t name [20]; int age, salary; swscanf(mystr, L"%ls %i %i", name, &age, &salary); wprintf(L"%ls is %i years old and earns %i dollars.\n", name, age, salary); return 0; }
The output of the above code will be:
John is 25 years old and earns 3000 dollars.
Example: using * sub-specifier
Consider one more example where sub-specifier * is used to ignore the data while using this function.
#include <wchar.h> int main (){ wchar_t mystr [] = L"John => 25 => 3000"; wchar_t name [20]; int age, salary; swscanf(mystr, L"%ls %*ls %i %*ls %i", name, &age, &salary); wprintf(L"%ls is %i years old and earns %i dollars.\n", name, age, salary); return 0; }
The output of the above code will be:
John is 25 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 <wchar.h> Library