| | |
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 |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






