What is the swscanf function? How can I use swscanf in a C++ app? Where can I find format specifiers for the swccanf function? What is the syntax of swscanf? Where can I find full examples of using the swscanf function in a C++ app?
Table of Contents
What is the swscanf function?
The swscanf function is an old C function (since C95) that reads data from a variety of sources, scans and formats input from a string. In other terms, swscanf interprets it according to format
and stores the results into given locations. We can use swscanf in C++. Reads the data from null-terminated wide string buffer
. swscanf attempts to read at end-of-string, it returns EOF. Note: For details on format specifiers, see scanf, wscanf.
Syntax of swscanf (Since C99):
1 2 3 |
int swscanf( const wchar_t *restrict buffer, const wchar_t *restrict format, ... ); |
What can swscanf do for my C++ app?
swscanf scans a series of input fields, one character at a time, reading from a string. Then each field is formatted according to a format specifier passed to swscanf in the format string pointed to by format. Finally, swscanf stores the formatted input at an address passed to it as an argument following format. There must be the same number of format specifiers and addresses as there are input fields. Format specifiers can be found here. For example, we can use “%d” or “%i” to read (scan) decimal / integer numbers, we can use “%f” to read floating point numbers.
swscanf might stop scanning a particular field before it reaches the normal end-of-field (whitespace) character, or it might terminate entirely, for a number of reasons. See scanf for a discussion of possible causes.
What does value swscanf return?
On success, swscanf returns the number of input fields successfully scanned, converted, and stored; the return value does not include scanned fields that were not stored.
If swscanf attempts to read at end-of-string, it returns EOF.
On error (If no fields were stored), it returns 0.
Note: swscanf considers the “s” specifier to refer to a two-byte character (wchar_t), rather than requiring the capital “s” or “ls” to indicate that a pointer to a two-byte character is being passed.
Can I see a simple example of using swscanf function in a C++ app?
Here is a example to swscanf,
1 2 3 4 5 6 |
wchar_t str[] = L"23 45"; int x, y; swscanf( str, L"%d %d", &x, &y); |
Is there full C++ app examples of using the swscanf function?
Here is a full example to swscanf. We can use swscanf in different input formats. For example if there is a wchar_t string with two numbers separated by a space, we can read them as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> int main() { wchar_t str[] = L"23 45"; int x, y; swscanf( str, L"%d %d", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
If there is a wchar_t string with two numbers separated by a coma, we can read them as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> int main() { wchar_t str[] = L"23, 45"; int x, y; swscanf( str, L"%d, %d", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
We can use this function with wstring by using its c_str() method as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { std::wstring str = L"23 45"; int x, y; swscanf( str.c_str(), L"%d %d", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
In C programming, there are swscanf, fwscanf, swscanf, wscanf_s, fwscanf_s, swscanf_s function for the different requirements, can be used in C++.
Note that, In modern C++ we can use std::wcin to read values from an input. In a modern way, we can convert std::wstring to std::wstringstream then we can get parameters that we need.
C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.