943,900 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 758
  • C++ RSS
Jan 20th, 2009
0

Doing something stupid with stringstream

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

C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  1. Filename: Example.txt
  2. Integer: 65535
  3. String:
  4. Double: 4.86439e-270

Can anyone spot the stupid bug?

Thanks,

Dave
Similar Threads
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jan 20th, 2009
1

Re: Doing something stupid with stringstream

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:
C++ Syntax (Toggle Plain Text)
  1. linestream.clear();
  2. linestream << line;
  3. ...
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 20th, 2009
0

Re: Doing something stupid with stringstream

That's what I thought I was doing with
C++ Syntax (Toggle Plain Text)
  1. linestream.str("");

But I changed all of those to

C++ Syntax (Toggle Plain Text)
  1. linestream.clear()

And I get the same garbage results.

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jan 20th, 2009
1

Re: Doing something stupid with stringstream

I get correct results with the following code. The only changes were the ones you claim to have made:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 20th, 2009
0

Re: Doing something stupid with stringstream

Just tested it with the changes mentioned, and it works for me too.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Jan 20th, 2009
0

Re: Doing something stupid with stringstream

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

I didn't gaurd it with
C++ Syntax (Toggle Plain Text)
  1. if(fin == NULL)

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

Sorry for the silly mistake.

Dave
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Jan 20th, 2009
0

Re: Doing something stupid with stringstream

I'd think about how to check if a file is really open
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: how can a "scanf" input be a character when printed?
Next Thread in C++ Forum Timeline: write file in Linux using Multithreading





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC