Write to a stringstream and then read from it

Is there a way to write to a stringstream and then read from it (including a space charater or empty)?

My failed attempt follows.
tellp() works as expected until a space character is written to stream.
The space causes tellp() to fail.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <sstream>

int main()
{
	std::streampos pos1, pos2, pos3;
	std::stringstream results;
	std::string result;
	char input [] = {'a', 'b', ' ', 'c'};

	for (int i=0; i<3; i++)
	{
		pos1 = results.tellp();			// save pos1
		results << input[i];			// write
		results << std::endl; 			// will need endl to read line

		pos2 = results.tellp();			// save to pos2
		results.seekg(pos1, std::ios::beg);	// go back to pos1
		results >> result;			// read line
		results.seekp(pos2, std::ios::beg);	// restore to pos2

		pos3 = results.tellp();			// pos3 should be same as pos2

		std::cout << "input=" << input[i]	// see what we have
		       	<< ", pos1=" << pos1 << ", pos2=" << pos2 << ", pos3=" << pos3
			<< ", result=[" << result << "]"
		       	<< ", results={" << results.str() << "}\n";
	}
}


Here is the output:
input=a, pos1=0, pos2=2, pos3=2, result=[a], results={a
}
input=b, pos1=2, pos2=4, pos3=4, result=[b], results={a
b
}
input= , pos1=4, pos2=6, pos3=-1, result=[b], results={a
b

}
input=c, pos1=-1, pos2=-1, pos3=-1, result=[b], results={a
b

}

Output is as expected for inputs 'a' and 'b', but input ' ' has pos3=-1 which indicates a tellp() fail.
An empty character also causes tellp() to fail.
http://www.cplusplus.com/reference/ostream/ostream/tellp/

Thank you for reading my post.
Last edited on
On line 18 why are you trying to reposition the get pointer?

I suggest you run the program through your debugger. Watch the variables and the state of the stream as you single step through the program.
the >> operator skips whitespace so you might want to try get() instead
Last edited on
jlb:
line 18 repositions the get pointer so it can read the character that was written on line 14.

Yanson:
using get() on line 19 fixed it. Thank you so much.

The following code works as intended:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <sstream>

int main()
{
	std::streampos pos1, pos2, pos3;
	std::stringstream results;
	std::string result;
	char input [] = {'a', 'b', ' ', 'c'};

	for (int i=0; i<4; i++)
	{
		pos1 = results.tellp();			// save pos1
		results << input[i];			// write
		results << std::endl; 			// will need endl to read line

		pos2 = results.tellp();			// save to pos2
		results.seekg(pos1, std::ios::beg);	// go back to pos1
		result = results.get();			// read line
		results.seekp(pos2, std::ios::beg);	// restore to pos2

		pos3 = results.tellp();			// pos3 should be same as pos2

		std::cout << "input=" << input[i]	// see what we have
		       	<< ", pos1=" << pos1 << ", pos2=" << pos2 << ", pos3=" << pos3
			<< ", result=[" << result << "]"
		       	<< ", results={" << results.str() << "}\n";
	}
}

output:
input=a, pos1=0, pos2=2, pos3=2, result=[a], results={a
}
input=b, pos1=2, pos2=4, pos3=4, result=[b], results={a
b
}
input= , pos1=4, pos2=6, pos3=6, result=[ ], results={a
b

}
input=c, pos1=6, pos2=8, pos3=8, result=[c], results={a
b

c
}
Topic archived. No new replies allowed.