943,809 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 794
  • C++ RSS
May 27th, 2009
0

Messing up stringstream

Expand Post »
Hey guys,

Below code isn't working as I expect it to do. I expect it to read all params when 6 are given, buuuuuuuuuuuut it only reads one, the rest remains zero. It does enter the case, but the stringstream buf seems empty. What am I doing wrong?

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. #define SCREAM cout << "OMAGAWD" << endl
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10. // <src> <dest> [<offset> [<bytes, 0 = all> [<offset>]]
  11. //writes bytes bytes from src+offset to dest+offset
  12.  
  13. size_t in_offset;
  14. size_t bytes;
  15. size_t out_offset;
  16.  
  17. in_offset = bytes = out_offset = 0;
  18.  
  19. stringstream buf;
  20.  
  21. switch (argc)
  22. {
  23. case 6:
  24. SCREAM;
  25. buf.str(argv[5]);
  26. buf >> out_offset;
  27.  
  28. case 5:
  29. SCREAM;
  30. buf.str(argv[4]);
  31. buf >> bytes;
  32.  
  33. case 4:
  34. SCREAM;
  35. buf.str(argv[3]);
  36. buf >> in_offset;
  37.  
  38. default:
  39. ifstream in(argv[1], ios::binary);
  40. ofstream out(argv[2], ios::binary);
  41. break;
  42. }
  43.  
  44. cout << "Writing from " << argv[1] << " + " << in_offset << " to " << argv[2] << " + " << out_offset << " for " << bytes << " bytes" << endl;
  45.  
  46. return 0;
  47. }

Example params:

program.exe aap noot 1 2 3
example output:

C++ Syntax (Toggle Plain Text)
  1. OMAGAWD
  2. OMAGAWD
  3. OMAGAWD
  4. Writing from aap + 0 to noot + 3 for 0 bytes

Thanks in advance,
Last edited by Clockowl; May 27th, 2009 at 6:08 pm.
Similar Threads
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 2008
May 27th, 2009
3

Re: Messing up stringstream

The state of the stream and the contents of the buffer are separate. Even though you reset the buffer to a new string, the stream is still in an end-of-file state from the previous read.

You'll notice that out_offset is correctly set to 3 because the stream starts off in a good state when you create it. But the first time you read to the end of the initial buffer, the eofbit is set and changing the contents of the buffer won't make a difference.

Do a buf.clear(); to reset the stream state.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 27th, 2009
0

Re: Messing up stringstream

Thanks Narue!
Reputation Points: 69
Solved Threads: 28
Posting Whiz
Clockowl is offline Offline
376 posts
since May 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: Program without main()
Next Thread in C++ Forum Timeline: Problem writing to a binary file





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


Follow us on Twitter


© 2011 DaniWeb® LLC