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?

#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:

OMAGAWD
OMAGAWD
OMAGAWD
Writing from aap + 0 to noot + 3 for 0 bytes

Thanks in advance,

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.

commented: To the point and constructive. +2

Thanks Narue!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.