C++ Tutorials


String Streams

Tutorials > C++ > String Streams

View Full Source


Sometimes you may have a string that consists of a few pieces of data eg. 3 integers separated by spaces. You could look at each character, identifying spaces and then extracting each piece of data. This would make your code more complex than it should be.

What is a String Stream?

In the previous 2 tutorials, we dealt with file streams. This allowed us to use the redirection operators to extract and insert data. Instead of the data coming from a file, we can now extract the data from a string.

Contents of main.cpp :

#include <iostream>
#include <stdlib.h>

The following includes allows us to create a stringstream object and allow us to use the C functions respectively.

#include <sstream>
#include <stdio.h>

using namespace std;

int main()
	int a, b, c;

C does not have any string stream specifically. To achieve something similar, you can use the sscanf function. This works in the same way as the scanf function except that the first parameter is used to specify the string to look at. The code below extracts 3 integers from a character string.

	// C String Streams
	char cstr[] = "Data : 56 43 23";

	sscanf(cstr, "Data : %d %d %d", &a, &b, &c);

	printf("%d %d %d\n", a, b, c);

In C++, we create a stringstream object. The string variable being looked at can be passed to the object on creation as shown below. This automatically assigns the string to the stringstream and allows you to read from it.

	// C++ String Streams
	a = b = c = 0;

	string cppstr = "Data : 56 43 23";

	stringstream ss(cppstr);

A useful function used below is the isdigit function. This function takes a character as a parameter and returns true if the character is a digit or false if it is not.

A get method is available when using a stringstream object. This returns the next character in the stringstream and moves the current position forward by one character.

If you want to look at the next character but you do not want to move the current position in the stringstream forward, you can use the peek method which will only return the next character in the stringstream.

The code below will continue to read characters from the stringstream until a digit is found. We use the peek method to stop this section of code from moving past the first digit.

	while (!isdigit(ss.peek()))

As was said above, you can now use the stringstream in the same way as the cout and cin objects. The code below stores the 3 integer values into their respective variables.

	ss >> a >> b >> c;

	cout << a << " " << b << " " << c << endl;


	return 0;

You should now have the ability to extract data from strings. This is useful as sometimes you will find that you have a few pieces of data stored within one string.

Please let me know of any comments you may have : Contact Me

Source Files : Visual Studio Dev-C++ Unix / Linux

< Tutorial 38 - Reading Text Files Tutorial 40 - Memory Allocation >

Back to Top

All Rights Reserved, © Zeus Communication, Multimedia & Development 2004-2005

Read the Disclaimer