Site icon Learn C++

What Is The sscanf Function In C++ And How Can I Use It?

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.

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

[crayon-661ee2ad445ec573689584/]

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,

[crayon-661ee2ad445f1207113344/]

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,

[crayon-661ee2ad445f8653221136/]

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

[crayon-661ee2ad445f9422203307/]

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

[crayon-661ee2ad445fb095155915/]

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.

Exit mobile version