std::vscanf, std::vfscanf, std::vsscanf
Min standard notice:
Header: <cstdio>
Reads data from a variety of sources, interprets it according to format and stores the results into locations defined by vlist.
# Declarations
int vscanf( const char* format, std::va_list vlist );
(since C++11)
int vfscanf( std::FILE* stream, const char* format, std::va_list vlist );
(since C++11)
int vsscanf( const char* buffer, const char* format, std::va_list vlist );
(since C++11)
# 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.
# 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 to not invoke va_end, and it must be done by the caller.
# Example
#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';
}
}