Doing something stupid with stringstream

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Doing something stupid with stringstream

 
0
  #1
Jan 20th, 2009
I put together this little example to test my sanity, and it failed!

  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. /* Example.txt
  10. 23
  11. test
  12. 4.5
  13. */
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. string Filename = argv[1];
  18. cout << "Filename: " << Filename << endl;
  19.  
  20. int Integer;
  21. string String;
  22. double Double;
  23.  
  24. ifstream fin(Filename.c_str());
  25.  
  26. string line;
  27. stringstream linestream;
  28.  
  29. getline(fin, line);
  30. linestream.str("");
  31. linestream << line;
  32. linestream >> Integer;
  33.  
  34. getline(fin, line);
  35. linestream.str("");
  36. linestream << line;
  37. linestream >> String;
  38.  
  39. getline(fin, line);
  40. linestream.str("");
  41. linestream << line;
  42. linestream >> Double;
  43.  
  44. fin.close();
  45.  
  46. cout << "Integer: " << Integer << endl;
  47. cout << "String: " << String << endl;
  48. cout << "Double: " << Double << endl;
  49.  
  50. return 0;
  51. }

The output is

  1. Filename: Example.txt
  2. Integer: 65535
  3. String:
  4. Double: 4.86439e-270

Can anyone spot the stupid bug?

Thanks,

Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Doing something stupid with stringstream

 
0
  #2
Jan 20th, 2009
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:
  1. linestream.clear();
  2. linestream << line;
  3. ...
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Doing something stupid with stringstream

 
0
  #3
Jan 20th, 2009
That's what I thought I was doing with
  1. linestream.str("");

But I changed all of those to

  1. linestream.clear()

And I get the same garbage results.

Dave
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Doing something stupid with stringstream

 
0
  #4
Jan 20th, 2009
I get correct results with the following code. The only changes were the ones you claim to have made:
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <sstream>
  6.  
  7. using namespace std;
  8.  
  9. /* Example.txt
  10. 23
  11. test
  12. 4.5
  13. */
  14.  
  15. int main(int argc, char *argv[])
  16. {
  17. string Filename = argv[1];
  18. cout << "Filename: " << Filename << endl;
  19.  
  20. int Integer;
  21. string String;
  22. double Double;
  23.  
  24. ifstream fin(Filename.c_str());
  25.  
  26. string line;
  27. stringstream linestream;
  28.  
  29. getline(fin, line);
  30. linestream << line;
  31. linestream >> Integer;
  32.  
  33. getline(fin, line);
  34. linestream.clear();
  35. linestream << line;
  36. linestream >> String;
  37.  
  38. getline(fin, line);
  39. linestream.clear();
  40. linestream << line;
  41. linestream >> Double;
  42.  
  43. fin.close();
  44.  
  45. cout << "Integer: " << Integer << endl;
  46. cout << "String: " << String << endl;
  47. cout << "Double: " << Double << endl;
  48.  
  49. return 0;
  50. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Doing something stupid with stringstream

 
0
  #5
Jan 20th, 2009
Just tested it with the changes mentioned, and it works for me too.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Doing something stupid with stringstream

 
0
  #6
Jan 20th, 2009
haha OH NO! The file was in the wrong directory... it works now.

I didn't gaurd it with
  1. if(fin == NULL)

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

Sorry for the silly mistake.

Dave
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 670
Reputation: Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough Freaky_Chris is a jewel in the rough 
Solved Threads: 113
Freaky_Chris's Avatar
Freaky_Chris Freaky_Chris is offline Offline
Practically a Master Poster

Re: Doing something stupid with stringstream

 
0
  #7
Jan 20th, 2009
I'd think about how to check if a file is really open
Knowledge is power -- But experience is everything
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC