954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Doing something stupid with stringstream

I put together this little example to test my sanity, and it failed!

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

/* Example.txt
23
test
4.5
*/

int main(int argc, char *argv[])
{
	string Filename = argv[1];
	cout << "Filename: " << Filename << endl;

	int Integer;
	string String;	
	double Double;

	ifstream fin(Filename.c_str());

	string line;
	stringstream linestream;
		
	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> Integer;	

	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> String;

	getline(fin, line);
	linestream.str("");
	linestream << line;
	linestream >> Double;

	fin.close();

	cout << "Integer: " << Integer << endl;
	cout << "String: " << String << endl;
	cout << "Double: " << Double << endl;
	
	return 0;
}


The output is

Filename: Example.txt
Integer: 65535
String: 
Double: 4.86439e-270


Can anyone spot the stupid bug?

Thanks,

Dave

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

A stringstream is still a stream. When you read to end-of-file, you need to clear the state before you can do anything substantial with the stream again:

linestream.clear();
linestream << line;
...
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

That's what I thought I was doing with

linestream.str("");


But I changed all of those to

linestream.clear()


And I get the same garbage results.

Dave

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

I get correct results with the following code. The only changes were the ones you claim to have made:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

/* Example.txt
23
test
4.5
*/

int main(int argc, char *argv[])
{
	string Filename = argv[1];
	cout << "Filename: " << Filename << endl;

	int Integer;
	string String;	
	double Double;

	ifstream fin(Filename.c_str());

	string line;
	stringstream linestream;
		
	getline(fin, line);
	linestream << line;
	linestream >> Integer;	

	getline(fin, line);
	linestream.clear();
	linestream << line;
	linestream >> String;

	getline(fin, line);
	linestream.clear();
	linestream << line;
	linestream >> Double;

	fin.close();

	cout << "Integer: " << Integer << endl;
	cout << "String: " << String << endl;
	cout << "Double: " << Double << endl;
	
	return 0;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Just tested it with the changes mentioned, and it works for me too.

mahlerfive
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 33
Solved Threads: 18
 

haha OH NO! The file was in the wrong directory... it works now.

I didn't gaurd it with

if(fin == NULL)


because it was just a toy example... but clearly I should have!

Sorry for the silly mistake.

Dave

daviddoria
Posting Virtuoso
1,996 posts since Feb 2008
Reputation Points: 437
Solved Threads: 204
 

I'd think about how to check if a file is really open :)

Freaky_Chris
Master Poster
702 posts since Apr 2008
Reputation Points: 325
Solved Threads: 118
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You