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

The Modern Way To Read Formatted Wide Strings In A C++ App

The Modern Way To Read Formatted Wide String In A C++ App

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.

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,

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,

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,

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,

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.

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.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

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

How To Use Basic Methods Of Strings In C++?