473,385 Members | 1,727 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

std::stringstream::seekg

Hi again,

In the following program I expected step 3 to assign the values 1 and
2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does
not move the read position back to the beginning of the stream as
expected.

//-----------------------------------------------------------------------------
#include <iostream>
#include <sstream>

int main()
{
//1. create a buffer containing "1,2"
std::stringstream ss;
ss << 1 << " " << 2;
std::cout << "\n" << ss.str();

//2. read the values 1 & 2 from the buffer and assign to iVal1 &
iVal2
int iVal1=-1, iVal2=-1;
ss >> iVal1 >> iVal2;
std::cout << "\n" << iVal1 << "," << iVal2;

//3. now move the read position back to the beginning of the stream
and read
//the values again
ss.seekg(0, std::ios::beg);
iVal1=-1; iVal2=-1;
ss >> iVal1 >> iVal2;
std::cout << "\n" << iVal1 << "," << iVal2;

//4. write the buffer to make sure it still contains "1 2" [OK]
cout << "\n" << ss.str();

return 0;
}

//-------------------------------------------------------------------------------

output from above is:

1,2
1,2
-1,-1
1,2
How can i move the read position back to the start of the buffer?

thanks
Aug 11 '05 #1
4 15839
You have

std::cout << ss.tellg();

this will print out the current read position, place it directly after
ss.seekg(0, std::ios::beg);

and it prints out 0

Aug 11 '05 #2
On 11 Aug 2005 04:00:55 -0700, sh*************@hotmail.com wrote:
You have

std::cout << ss.tellg();

this will print out the current read position, place it directly after
ss.seekg(0, std::ios::beg);

and it prints out 0

so why am I not getting the expected result?
Aug 11 '05 #3
Actually no you haven't add this ss.clear(); before your ss.seekg(0,
std::ios::beg); statement
You have to clear the state of the stream [eofbit or failbit may have
been set] Probably the former since you already read from the stream
and you have to clear it before you can reset the read position.

ss.clear();
ss.seekg(0, std::ios::beg);

HTH
SMJT

Aug 11 '05 #4
On 11 Aug 2005 04:15:30 -0700, sh*************@hotmail.com wrote:
Actually no you haven't add this ss.clear(); before your ss.seekg(0,
std::ios::beg); statement
You have to clear the state of the stream [eofbit or failbit may have
been set] Probably the former since you already read from the stream
and you have to clear it before you can reset the read position.

ss.clear();
ss.seekg(0, std::ios::beg);

HTH
SMJT


That's it! Thanks very much
Aug 11 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: lallous | last post by:
Hello, I'm new to streams, so can someone tell me what to use if I want such functionality: mem_stream s; s.write(myBuffer, bufferlength); s.seek(100); s.read(readBuff, 100);
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
9
by: Robin | last post by:
// Pls note that test.txt exists. #include <iostream> #include <fstream> #include <string> using namespace std; int main() { fstream fs("test.txt", ios_base::in | ios_base::app);
4
by: clb | last post by:
I am trying to use stringstreams and my book doesn't cover the included methods. For example, if I init a istringstream on a string and suck all the data out, then put more stuff into the string,...
3
by: whatdoineed2do | last post by:
hi, i have come aross a strange problem with a bit of code that uses a ostringstream to build up a string and i extract the string into a user buf via the sgetn() call instead of via the str()...
8
by: sparkydarky | last post by:
I have a bunch of strings in a database that have in them a number I want to extract. Here's the example code I've been working with: AnsiString tmp = "AMjsdfdsdfj 457756 some description"; int...
0
by: whatdoineed2do | last post by:
hi, i'm trying avoid uncessary copy of data whilst using the stringstream so i am using the sgetn() to copy the data into a buf as apposed to getting the same data via the .str() which will...
4
MrPickle
by: MrPickle | last post by:
I am tokenizing a line using a stringstream and a vector but the stringstream seems to be repeating the first word, here's my code: std::stringstream ss; std::vector<std::string> LineVec;...
3
MrPickle
by: MrPickle | last post by:
I have a tag like so: <foo> and I am trying to extract 'foo' from this. I'm using a stringstream in a while loop but it seems to fail with the first line, then it works with every other line. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.