Messing up stringstream

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

Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Messing up stringstream

 
0
  #1
May 27th, 2009
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?

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,739
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: 739
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Messing up stringstream

 
3
  #2
May 27th, 2009
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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 376
Reputation: Clockowl is on a distinguished road 
Solved Threads: 27
Clockowl's Avatar
Clockowl Clockowl is offline Offline
Posting Whiz

Re: Messing up stringstream

 
0
  #3
May 27th, 2009
Thanks Narue!
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