How can we read a formatted string in a C++ app? How can I convert a string to a stringstream ? What is string? What is std::stringstream
? What are the methods that can be used to read the formatted strings? Is there a simple example on how to read a formatted string ? Is there a full C++ app example showing reading of a formatted string in C++? Let’s answer these questions.
When we try to analyze data files such as .csv or .txt files, we can use different C and C++ functions and methods to read formatted text lines. There can be used some modern ways in C++ besides the C scan function. In this post, we will explain one of the ways that can be used.
Table of Contents
What is a string?
Generally, as an introduction to C++, in addition to int, float, double there is another data type we should know. In C++ there are several typedefs of common character types provided: String types are defined in header <string>.
string has methods to assign, copy, align, replace, or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. Here are the string types defined in the std namespace with their char type and C++ standard.
What are the methods a C++ app can use to read formatted strings?
In C programming, there are scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s functions for the different requirements, can be used in C++. Actually, there is no single modern function in C++ that can be used in all string formats. We can use std::cin to read values from input. In a modern way, we can convert string to std::stringstream then we can get parameters that we need. Now let’s see how to use stringstream.
What is stringstream in a C++ app?
std::stringstream is a stream class to operate on strings implemented from the class template std::basic_stringstream that has input and output operations on string-based streams. std::stringstream is an instance of std::basic_string and performs the input and output operations on it.
Is there a simple example showing how to read a formatted string ?
Here is an example to read x and y values from a string,
1 2 3 4 5 6 7 8 9 |
char c; float x, y; std::string formated_str = "-5.2 , -8.3"; std::stringstream sstream(formated_str); sstream >> x >> c >> y ; |
Here is a full C++ app example showing how to read formatted strings
This example below shows how you can convert a string to a stringstream and how to read members of a stringstream,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <iostream> #include <sstream> int main() { char c; float x, y; std::string formated_str = "-5.2 , -8.3"; std::stringstream sstream(formated_str); sstream >> x >> c >> y ; std::cout << "x:" << x << " and y:" << y << std::endl; getchar(); return 0; } |
Note that here we used char to read a character and spaces between ‘,’ characters are eliminated automatically. If you have different sizes of texts between numbers you need to develop reading delimited tokens of string. There is a std::istringstream that can be used as below,
1 2 3 4 |
std::string str = "6.9, 1.28"; std::istringstream iss( str, std::ios::binary ); |
In C++ Builder there are Delimiters that can be used with TStringList, read this post below if you need to analyze text lines with delimiters as below,
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.