std::vscanf...std::vsscanf(3)
๐ std::vscanf, std::vfscanf, std::vsscanf โ C++ Standard Libary ๐
okay bestie, we're deep in the C++ Standard Libary and this page is defined in header <cstdio>. It's the variadic scanf trio, and it's giving "read all the things and stash them through vlist" energy. ๐
โจ NAME โจ
std::vscanf, std::vfscanf, std::vsscanf
๐ธ Synopsis ๐ธ
Defined in header <cstdio>
int vscanf( const char* format, std::va_list vlist );
(1) (since C++11)
int vfscanf( std::FILE* stream, const char* format, std::va_list vlist );
(2) (since C++11)
int vsscanf( const char* buffer, const char* format, std::va_list vlist );
(3) (since C++11)
Reads data from a variety of sources, interprets it according to format and stores the results into locations defined by vlist.
- Reads the data from
stdin. - Reads the data from file stream
stream. - Reads the data from null-terminated character string
buffer.
๐ Parameters ๐
streamโ input file stream to read frombufferโ pointer to a null-terminated character string to read fromformatโ pointer to a null-terminated character string specifying how to read the inputvlistโ variable argument list containing the receiving arguments.
The format string consists of
- non-whitespace multibyte 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. - whitespace characters: any single whitespace character in the format string consumes all available consecutive whitespace characters from the input (determined as if by calling
isspacein a loop). Note that there is no difference between"\n"," ","\t\t", or other whitespace in the format string. - 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 table below).
- conversion format specifier.
- introductory
Format specifier key ๐
The following format specifiers are available. Length modifier key: hh (C++11), h (C++11), (none), l (C++11), ll (C++11), j (C++11), z (C++11), t (C++11), L.
Integer type mapping (the combined cell) ๐งฎ
For d, i, u, o, x, and X, the page's combined argument-type cell gives:
hhโsigned char*orunsigned char*hโshort*orunsigned short*(none)โint*orunsigned int*lโlong*orunsigned long*llโlong long*orunsigned long long*jโintmax_t*oruintmax_t*zโsize_t*tโptrdiff_t*LโN/A
The signed conversions d and i take the signed side of those or pairs; u, o, x, and X take the unsigned side. The page literally shows the combined cell this way, bestie โ I'm keeping the ors. ๐ซถ
The actual specifiers ๐
%โ Matches literal%. Argument type:N/Aforhh,N/Aforh,N/Afor(none),N/Aforl,N/Aforll,N/Aforj,N/Aforz,N/Afort,N/AforL.cโ Matches a character or a sequence of characters. If a width specifier is used, matches exactly width 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 type: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 least width+1 characters). Argument type: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 character 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 type: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 bystrtolwith the value10for the base argument. Argument type: use the combined integer type mapping above.iโ Matches an integer. The format of the number is the same as expected bystrtolwith the value0for the base argument (base is determined by the first characters parsed). Argument type: use the combined integer type mapping above.uโ Matches an unsigned decimal integer. The format of the number is the same as expected bystrtoulwith the value10for the base argument. Argument type: use the combined integer type mapping above.oโ Matches an unsigned octal integer. The format of the number is the same as expected bystrtoulwith the value8for the base argument. Argument type: use the combined integer type mapping above.x,Xโ Matches an unsigned hexadecimal integer. The format of the number is the same as expected bystrtoulwith the value16for the base argument. Argument type: use the combined integer type mapping above.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. Argument type: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.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 bystrtof. Argument type: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 type: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 isspace) before attempting to parse the input. These consumed characters do not count towards the specified maximum field width.
The conversion specifiers lc, ls, and l[ perform multibyte-to-wide character conversion as if by calling mbrtowc 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.
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.
๐ Notes ๐
All these functions invoke va_arg at least once, the value of arg is indeterminate after the return. These functions do not invoke va_end, and it must be done by the caller.
โจ EXAMPLE โจ
okay bestie, this one's a lil demo of wrapping vsscanf in your own checked function ๐
it's giving "safety wrapper realness" and honestly? iconic.
// Run this code
#include <cstdarg>
#include <cstdio>
#include <iostream>
#include <stdexcept>
void checked_sscanf(int count, const char* buf, const char *fmt, ...)
{
std::va_list ap;
va_start(ap, fmt);
if (std::vsscanf(buf, fmt, ap) != count)
throw std::runtime_error("parsing error");
va_end(ap);
}
int main()
{
try
{
int n, m;
std::cout << "Parsing '1 2'... ";
checked_sscanf(2, "1 2", "%d %d", &n, &m);
std::cout << "success\n";
std::cout << "Parsing '1 a'... ";
checked_sscanf(2, "1 a", "%d %d", &n, &m);
std::cout << "success\n";
}
catch (const std::exception& e)
{
std::cout << e.what() << '\n';
}
}
๐ Output
Parsing '1 2'... success
Parsing '1 a'... parsing error
๐ธ SEE ALSO ๐ธ
like, if you wanna go deeper down the scanf rabbit hole, these are the besties to check out ๐ซถ
scanfโ reads formatted input from stdin, a file stream or a bufferfscanfโ (function)sscanfvprintfvfprintfโ prints formatted output to stdout, a file stream or a buffervsprintfโ using variable argument listvsnprintfโ (function) (C++11)- C documentation for
vscanf,vfscanf,vsscanf
๐ source: cppreference.com ยท version date: 2024.06.10 ยท page: std::vscanf...std::vsscanf(3) ๐
no cap, you're gonna slay this whole scanf situation now โจ๐ฆ
๐ฆ 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