How can we read a formatted wstring in a modern C++ app? How can I convert a wstring to a wstringstream? What is std::wstringstream
? What are the methods that can be used to read a formatted string in general? Is there a simple example of how to read formatted wide string? Is there a full C++ example of reading formatted wide strings in a C++ app? Let’s answer these questions.
When we try to analyze data files which are .csv or .txt files, we can use different C and C++ functions and methods to read formatted text lines. There are some modern ways in C++ besides the C scan function. In this post, we will explain one of the ways.
Table of Contents
What is a wstring?
Modern C++ uses Wide Strings and Unicode Strings to support worldwide languages. Wide Strings (std::wstring) uses wcha_tr as the character type which means they are 2-bytes chars and they are an instantiation of the basic_string class template. In C++, there are several type definitions for common character types and one of them is std::wstring types that are defined in header <string>.
wstrings are the string class for 2-bytes characters represented with wstring and alphanumeric characters are stored and displayed in wide string forms. In other terms, wstring stores for the alphanumeric text with 2-byte chars, called wchar_t. Wide Strings are the instantiation of the basic_string class template that uses wchar_t as the character type. In a modern C++ app, we can simply define a wide string with L” ” literal as below,
1 2 3 |
std::wstring wstr = L"This is a String"; |
Wide Strings (wstring) are a class that contains arrays of wide characters with useful methods, and we can access, or modify their characters easily. In C++, while wide string contents are defined between L” and ” with literal L, wide characters are defined between L’ and ‘ with literal L
The wstring has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can clear a wstring with clear(), erase(), resize() methods.
What are the general C++ methods used 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 and std::wcin to read values from an input string. In a modern way, we can convert std::string to std::stringstream, and std::wstring to std::wstringstream then we can get parameters that we need. Now let’s see how to use wstringstream.
What is wstringstream in a C++ app?
std::wstringstream is a stream class to operate on wide strings, it is implemented from the class template std::basic_stringstream that has input and output operations on the string-based streams. std::wstringstream is an instance of std::basic_wstring and performs the input and output operations on it.
Is there a simple example of how to read a formatted Wide string?
Here is an example to read x and y values from a string,
1 2 3 4 5 6 7 8 9 |
wchar_t c; float x, y; std::wstring formated_wstr = L"-5.2 , -8.3"; std::wstringstream wsstream(formated_wstr); wsstream >> x >> c >> y ; |
Is there a full C++ example showing how to read formatted Wide strings in a C++ app?
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() { wchar_t c; float x, y; std::wstring formated_wstr = L"-5.2 , -8.3"; std::wstringstream wsstream(formated_wstr); wsstream >> x >> c >> y ; std::wcout << L"x:" << x << L" and y:" << y << std::endl; getchar(); return 0; } |
Note that here we used wchar_t 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 wstring. There is a std::wistringstream that can be used as the same way with wstring as below,
1 2 3 4 |
std::wstring wstr = L"6.9, 1.28"; std::wistringstream iss( wstr, std::ios::binary ); |
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.