| | |
Messing up stringstream
Please support our C++ advertiser: Intel Parallel Studio Home
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. I'm here to prove you wrong.
![]() |
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
| Thread Tools | Search this Thread |
add api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count data delete deploy desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib library linkedlist linker linux list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive return simple string strings struct studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






