C++C++11C++14C++17Learn C++

How To Use The All-Powerful swscanf function In Your C++ App

How To Use The All Powerful swscanf In Your C++ App

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?

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):

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,

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,

If there is a wchar_t string with two numbers separated by a coma, we can read them as below,

We can use this function with wstring by using its c_str() method as below,

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

C++C++11C++14C++17C++20Introduction to C++Learn C++Syntax

How To Use Basic Methods Of Strings In C++?