C++Introduction to C++Learn C++

What Is The Modern Way To Read Formatted Strings In A C++ App?

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.

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,

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,

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,

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.

close

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Join Two Wide Strings In Modern C++ Software

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

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

C++ComponentsLanguage FeatureLearn C++

How To Use A Sharpen Effect in C+ Software On Windows?

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

What Is The Erase WString Method In A Modern C++ App