What is sscanf function? How can I use sscanf in C++? Where can I find format specifiers for the sccanf function? What is the syntax of sscanf ? What is a simple example of sscanf function in C++? Is There Full Examples to sscanf Function in C++? Let’s answer these questions by learning sscanf and then using the C++ IDE to easily build C++ applications in the future.
Table of Contents
What is the sscanf function?
sscanf 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, sscanf interprets it according to format
and stores the results into given locations. We can use sscanf in C++. sscanf function reads the data from null-terminated character string buffer
and defined in header <stdio.h>
can be used in C++ applications. The end of the string is same as the end-of-file condition for fscanf
. Generally sscanf function is good to convert parameters listed in a single string or a string from a data file, thuss it can be very useful to obtain values from a string data, and we can save these values to a modern database.
What is the syntax of the sscanf function?
Syntax of sscanf (Since C99):
1 2 3 |
int sscanf( const char *restrict buffer, const char *restrict format, ... ); |
What does the sscanf function do?
The sscanf function 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 sscanf in the format string pointed to by format. Finally, sscanf 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 or integer numbers, we can use “%f” to read floating-point numbers.
sscanf 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.
On success, sscanf returns the number of input fields successfully scanned, converted, and stored; the return value does not include scanned fields that were not stored.
If sscanf attempts to read at end-of-string, it returns EOF. On error (If no fields were stored), it returns 0.
Note: sscanf 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.
What is a simple example of the sscanf function in C++?
Here is an example of sscanf,
1 2 3 4 5 |
std::string str = "23 45"; int x, y; sscanf( str.c_str(), "%d %d", &x, &y); |
Are there any full examples of using the sscanf function in C++?
We can use sscanf in different input formats. For example if there is a 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 |
#include <iostream> int main() { std::string str = "23 45"; int x, y; sscanf( str.c_str(), "%d %d", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
If there is a 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 |
#include <iostream> int main() { std::string str = "23, 45"; int x, y; sscanf( str.c_str(), "%d,%d", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
If there is a string with two floating 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 |
#include <iostream> int main() { std::string str = "23.1 45.8"; double x, y; sscanf( str.c_str(), "%lf %lf", &x, &y); std::cout << x << "," << y; getchar(); return 0; } |
In C programming, there are scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s functions for the different requirements, can be used in C++.
Note that in C++ we can use std::cin to read values from an input. In a modern way, we can convert string to std::stringstream 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.
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition