| | |
Messing up stringstream
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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?
Example params:
example output:
Thanks in advance,
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)
#include <iostream> #include <fstream> #include <sstream> using namespace std; #define SCREAM cout << "OMAGAWD" << endl int main(int argc, char *argv[]) { // <src> <dest> [<offset> [<bytes, 0 = all> [<offset>]] //writes bytes bytes from src+offset to dest+offset size_t in_offset; size_t bytes; size_t out_offset; in_offset = bytes = out_offset = 0; stringstream buf; switch (argc) { case 6: SCREAM; buf.str(argv[5]); buf >> out_offset; case 5: SCREAM; buf.str(argv[4]); buf >> bytes; case 4: SCREAM; buf.str(argv[3]); buf >> in_offset; default: ifstream in(argv[1], ios::binary); ofstream out(argv[2], ios::binary); break; } cout << "Writing from " << argv[1] << " + " << in_offset << " to " << argv[2] << " + " << out_offset << " for " << bytes << " bytes" << endl; return 0; }
Example params:
program.exe aap noot 1 2 3 example output:
C++ Syntax (Toggle Plain Text)
OMAGAWD OMAGAWD OMAGAWD Writing from aap + 0 to noot + 3 for 0 bytes
Thanks in advance,
Last edited by Clockowl; May 27th, 2009 at 6:08 pm.
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
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. New members chased away this month: 5
![]() |
Similar Threads
- Memory leaks through stringstream.str() (C++)
- Need help for linked list (C++)
- Conversion from Char* to int ? (C++)
- IE Messing up? (Viruses, Spyware and other Nasties)
- using stringstream for tokenizing a string (C++)
Other Threads in the C++ Forum
- Previous Thread: Program without main()
- Next Thread: use x64 gcc to compile elf_i386 code
Views: 404 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sort sorting spoonfeeding string strings struct temperature template templates text tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






