std::wscanf...std::swscanf(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– 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.

  1. Reads the data from stdin.
  2. Reads the data from file stream stream.
  3. 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 iswspace in 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 %s and %[ 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.

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/A for hh, N/A for h, N/A for (none), N/A for l, N/A for ll, N/A for j, N/A for z, N/A for t, and N/A for L.
  • 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 %s and %[, does not append the null character to the array. Argument types: N/A for hh; N/A for h; char* for (none); wchar_t* for l; N/A for ll; N/A for j; N/A for z; N/A for t; N/A for L.
  • 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 least width+1 characters). Argument types: N/A for hh; N/A for h; char* for (none); wchar_t* for l; N/A for ll; N/A for j; N/A for z; N/A for t; N/A for L.
  • [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 least width+1 characters). Argument types: N/A for hh; N/A for h; char* for (none); wchar_t* for l; N/A for ll; N/A for j; N/A for z; N/A for t; N/A for L.
  • d β€” matches a decimal integer. The format of the number is the same as expected by wcstol with the value 10 for the base argument. Argument types: signed char* for hh; short* for h; int* for (none); long* for l; long long* for ll; intmax_t* for j; signed size_t* for z; ptrdiff_t* for t; N/A for L.
  • i β€” matches an integer. The format of the number is the same as expected by wcstol with the value 0 for the base argument (base is determined by the first characters parsed). Argument types: same as d β€” signed char* for hh; short* for h; int* for (none); long* for l; long long* for ll; intmax_t* for j; signed size_t* for z; ptrdiff_t* for t; N/A for L.
  • u β€” matches an unsigned decimal integer. The format of the number is the same as expected by wcstoul with the value 10 for the base argument. Argument types: unsigned char* for hh; unsigned short* for h; unsigned int* for (none); unsigned long* for l; unsigned long long* for ll; uintmax_t* for j; size_t* for z; ptrdiff_t* for t; N/A for L.
  • o β€” matches an unsigned octal integer. The format of the number is the same as expected by wcstoul with the value 8 for the base argument. Argument types: same as u β€” unsigned char* for hh; unsigned short* for h; unsigned int* for (none); unsigned long* for l; unsigned long long* for ll; uintmax_t* for j; size_t* for z; ptrdiff_t* for t; N/A for L.
  • x, X β€” matches an unsigned hexadecimal integer. The format of the number is the same as expected by wcstoul with the value 16 for the base argument. Argument types: same as u β€” unsigned char* for hh; unsigned short* for h; unsigned int* for (none); unsigned long* for l; unsigned long long* for ll; uintmax_t* for j; size_t* for z; ptrdiff_t* for t; N/A for L.
  • 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 for n aren’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 by wcstof. Argument types: N/A for hh; N/A for h; float* for (none); double* for l; N/A for ll; N/A for j; N/A for z; N/A for t; long double* for L.
  • p β€” matches implementation defined character sequence defining a pointer. printf family of functions should produce the same sequence using %p format specifier. Argument types: N/A for hh; N/A for h; void** for (none); N/A for l; N/A for ll; N/A for j; N/A for z; N/A for t; N/A for L.

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 as std::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 from stdin, 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 on std::wscanf, std::fwscanf, std::swscanf and 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