If I use the uncommented 3 lines for getting an int out of a stringstream, the value in MPV is wrong. However, if I make a new stringstream, then it works fine. I thought setting .str("") was essentially resetting the stringstream?

line = "";
	getline(fin, line); //get fourth line
	
//these lines don't work
	LineStream.str("");
	LineStream << line;
	LineStream >> MPV;
	
	/*
//these lines do
	stringstream LineStream2;
	LineStream2 << line;
	LineStream2 >> MPV;
	*/

Thanks,

Dave

If I use the uncommented 3 lines for getting an int out of a stringstream, the value in MPV is wrong. However, if I make a new stringstream, then it works fine. I thought setting .str("") was essentially resetting the stringstream?

If the stream object is in error state, then you also need to call clear() in order to make the object functional again. Could that be the reason in this case?

Yep that fixed it - why would it have gone into an error state? I threw this together to answer someone's thread:

void SkipComments(const string &Filename)
{
	/* test.txt
	P3
	# example comment
	512 512
	255

	*/
	ifstream fin(Filename.c_str());

	if(fin == NULL)
		cout << "Cannot open file." << endl;

	char MV[2];//the first 2 ascii values
	int width, height;
	int MPV;//max pixel value

	string line;
		
	getline(fin, line); //get first line
		
	MV[0] = line[0];
	MV[1] = line[1];
	cout << "MV[0] = " << MV[0] << endl;
	cout << "MV[1] = " << MV[1] << endl;
	
	line = "";
	getline(fin, line); //get second line
	string FirstChar = line.substr(0, 1);

	if(FirstChar == "#")
		cout << "Skipped this line." << endl;
	
	line = "";
	getline(fin, line); //get third line
	stringstream LineStream;
	LineStream << line;
	LineStream >> width >> height;
	cout << "Width = " << width << endl;
	cout << "Height = " << height << endl;
	LineStream.str("");
	
	line = "";
	getline(fin, line); //get fourth line
		
	LineStream.str("");
	LineStream.clear();
	LineStream << line;
	LineStream >> MPV;
		
	cout << "MPV = " << MPV << endl;
	
}

It doesn't seem like there should be any problems with that?

Dave

>why would it have gone into an error state?
end-of-file counts as an error state in this case.

But it shouldn't see the end of file until after the 4th line is read, not the third, right?

>But it shouldn't see the end of file until after the 4th line is read, not the third, right?
I think you're confused about the meaning of end-of-file. You're thinking of the file itself, not the stringstream. The two objects are completely independent of each other and have no idea that the other exists.

The stringstream is only aware of the one string you used to initialize it. This is the line you use to initialize the stringstream:

512 512

As far as the stringstream object is concerned, that's the entirety of the file. After you extract the two integer values from the stringstream, the object goes into an end-of-file state.

commented: Great explanation! +2

Ah I see, great!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.