std::wscanf...std::swscanf(3)
π std::wscanf, std::fwscanf, std::swscanf(3) β C++ Standard Libary π
π NAME π
std::wscanf, std::fwscanf, std::swscanf β literally the trio: std::wscanf, std::fwscanf, std::swscanf. Bestie, this is the wide-character scanf family, and itβs giving βI read formatted input and Iβm not sorry.β π
π Synopsis π
Okay so first, the receipts β itβs defined in the header <cwchar>:
Defined in header <cwchar>
int wscanf( const wchar_t* format, ... ); (1)
int fwscanf( std::FILE* stream, const wchar_t* format, ... ); (2)
int swscanf( const wchar_t* buffer, const wchar_t* format, ... ); (3)
Reads data from the a variety of sources (yes, βthe aβ is in the source, Iβm not fixing it π), interprets it according to format, and stores the results into given locations.
- Reads the data from
stdin. - Reads the data from file stream
stream. - Reads the data from null-terminated wide string
buffer.
π«Ά Parameters π«Ά
streamβ input file stream to read from.bufferβ pointer to a null-terminated wide string to read from.formatβ pointer to a null-terminated wide string specifying how to read the input....β receiving arguments.
The format string consists of:
- non-whitespace wide characters except
%β each such character in the format string consumes exactly one identical character from the input stream, or causes the function to fail if the next character on the stream does not compare equal. no cap, itβs that strict. - whitespace characters β any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling
iswspacein a loop). Note that there is no difference between"\n"," ","\t\t", or other whitespace in the format string. literally no difference, bestie. - conversion specifications β each conversion specification has the following format:
- introductory
%character. - (optional) assignment-suppressing character
*. If this option is present, the function does not assign the result of the conversion to any receiving argument. - (optional) integer number (greater than zero) that specifies maximum field width, that is, the maximum number of characters that the function is allowed to consume when doing the conversion specified by the current conversion specification. Note that
%sand%[may lead to buffer overflow if the width is not provided. β οΈ - (optional) length modifier that specifies the size of the receiving argument, that is, the actual destination type. This affects the conversion accuracy and overflow rules. The default destination type is different for each conversion type (see the conversion breakdown below).
- conversion format specifier.
- introductory
The following format specifiers are available:
Length modifier vibes, in order: hh (C++11), h, (none), l, ll (C++11), j (C++11), z (C++11), t (C++11), L. For each conversion Iβm listing the argument type under those modifiers, exactly as the source table has them. N/A means the source table says N/A.
%β matches literal%. Argument types:N/Aforhh,N/Aforh,N/Afor (none),N/Aforl,N/Aforll,N/Aforj,N/Aforz,N/Afort, andN/AforL.cβ matches a character or a sequence of characters. If a width specifier is used, matches exactly width wide characters (the argument must be a pointer to an array with sufficient room). Unlike%sand%[, does not append the null character to the array. Argument types:N/Aforhh;N/Aforh;char*for (none);wchar_t*forl;N/Aforll;N/Aforj;N/Aforz;N/Afort;N/AforL.sβ matches a sequence of non-whitespace characters (a string). If width specifier is used, matches up to width or until the first whitespace character, whichever appears first. Always stores a null character in addition to the characters matched (so the argument array must have room for at leastwidth+1characters). Argument types:N/Aforhh;N/Aforh;char*for (none);wchar_t*forl;N/Aforll;N/Aforj;N/Aforz;N/Afort;N/AforL.[set]β matches a non-empty sequence of characters from set of characters. If the first character of the set is^, then all characters not in the set are matched. If the set begins with]or^]then the]character is also included into the set. It is implementation-defined whether the character-in the non-initial position in the scanset may be indicating a range, as in[0-9]. If width specifier is used, matches only up to width. Always stores a null character in addition to the characters matched (so the argument array must have room for at leastwidth+1characters). Argument types:N/Aforhh;N/Aforh;char*for (none);wchar_t*forl;N/Aforll;N/Aforj;N/Aforz;N/Afort;N/AforL.dβ matches a decimal integer. The format of the number is the same as expected bywcstolwith the value10for the base argument. Argument types:signed char*forhh;short*forh;int*for (none);long*forl;long long*forll;intmax_t*forj;signed size_t*forz;ptrdiff_t*fort;N/AforL.iβ matches an integer. The format of the number is the same as expected bywcstolwith the value0for the base argument (base is determined by the first characters parsed). Argument types: same asdβsigned char*forhh;short*forh;int*for (none);long*forl;long long*forll;intmax_t*forj;signed size_t*forz;ptrdiff_t*fort;N/AforL.uβ matches an unsigned decimal integer. The format of the number is the same as expected bywcstoulwith the value10for the base argument. Argument types:unsigned char*forhh;unsigned short*forh;unsigned int*for (none);unsigned long*forl;unsigned long long*forll;uintmax_t*forj;size_t*forz;ptrdiff_t*fort;N/AforL.oβ matches an unsigned octal integer. The format of the number is the same as expected bywcstoulwith the value8for the base argument. Argument types: same asuβunsigned char*forhh;unsigned short*forh;unsigned int*for (none);unsigned long*forl;unsigned long long*forll;uintmax_t*forj;size_t*forz;ptrdiff_t*fort;N/AforL.x,Xβ matches an unsigned hexadecimal integer. The format of the number is the same as expected bywcstoulwith the value16for the base argument. Argument types: same asuβunsigned char*forhh;unsigned short*forh;unsigned int*for (none);unsigned long*forl;unsigned long long*forll;uintmax_t*forj;size_t*forz;ptrdiff_t*fort;N/AforL.nβ returns the number of characters read so far. No input is consumed. Does not increment the assignment count. If the specifier has assignment-suppressing operator defined, the behavior is undefined. The argument-type cells fornarenβt rendered in the source table, so Iβm not inventing them β keeping it exact, bestie. πa,A(C++11),e,E,f,F,g,Gβ matches a floating-point number. The format of the number is the same as expected bywcstof. Argument types:N/Aforhh;N/Aforh;float*for (none);double*forl;N/Aforll;N/Aforj;N/Aforz;N/Afort;long double*forL.pβ matches implementation defined character sequence defining a pointer.printffamily of functions should produce the same sequence using%pformat specifier. Argument types:N/Aforhh;N/Aforh;void**for (none);N/Aforl;N/Aforll;N/Aforj;N/Aforz;N/Afort;N/AforL.
For every conversion specifier other than n, the longest sequence of input characters which does not exceed any specified field width and which either is exactly what the conversion specifier expects or is a prefix of a sequence it would expect, is whatβs consumed from the stream. The first character, if any, after this consumed sequence remains unread. If the consumed sequence has length zero or if the consumed sequence cannot be converted as specified above, the matching failure occurs unless end-of-file, an encoding error, or a read error prevented input from the stream, in which case it is an input failure. π«
All conversion specifiers other than [, c, and n consume and discard all leading whitespace characters (determined as if by calling iswspace) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width.
If the length specifier l is not used, the conversion specifiers c, s, and [ perform wide-to-multibyte character conversion as if by calling wcrtomb with an mbstate_t object initialized to zero before the first character is converted.
The conversion specifiers s and [ always store the null terminator in addition to the matched characters. The size of the destination array must be at least one greater than the specified field width. The use of %s or %[, without specifying the destination array size, is as unsafe as std::gets.
β οΈ danger, bestie: that%s/%[warning is not a vibe-check suggestion. It literally says βas unsafe asstd::gets.β Do not play with it. π
The correct conversion specifications for the fixed-width integer types (int8_t, etc) are defined in the header <cinttypes> (although SCNdMAX, SCNuMAX, etc is synonymous with %jd, %ju, etc).
There is a sequence point after the action of each conversion specifier; this permits storing multiple fields in the same βsinkβ variable.
When parsing an incomplete floating-point value that ends in the exponent with no digits, such as parsing "100er" with the conversion specifier %f, the sequence "100e" (the longest prefix of a possibly valid floating-point number) is consumed, resulting in a matching error (the consumed sequence cannot be converted to a floating-point number), with "r" remaining. Some existing implementations do not follow this rule and roll back to consume only "100", leaving "er", e.g. glibc bug 1765.
π Return value π
Number of arguments successfully read, or EOF if failure occurs before the first receiving argument was assigned.
πΈ Example πΈ
This section is incomplete. Reason: no example.
π See also π
vwscanf(C++11),vfwscanf(C++11),vswscanf(C++11) β (function) reads formatted wide character input fromstdin, a file stream or a buffer using variable argument list.- C documentation for
wscanf,fwscanf,swscanf.
π Category:
- Todo no example β bestie, this page is literally flagged as needing an example π it's giving "unfinished homework energy" fr fr. So if you were scrolling down hoping for one more little code snippet to β¨manifestβ¨ your understandingβ¦ not today, babe. The page itself is telling on itself here π
That's genuinely it β the whole rest of the page is just that one lonely little todo note, sitting there like the last fry at the bottom of the bag π
Lowkey iconic of it to end on a "we owe you an example" note ngl. If you want the real tea onstd::wscanf,std::fwscanf,std::swscanfand the whole scanf-format family, the full writeup lives over at http://cppreference.com β go give her a read π
Okay that's the whole page, start to finish, no cap π you made it to the end, main character behavior honestly π«Ά
cppreference.com Β· 2024.06.10 Β· std::wscanf...std::swscanf(3) β mwah π
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms