2

Here are two snippets of code which I at first thought should be equivalent:

{
    std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
    unsigned char count = 128;
    unsigned char read = 0;
    unsigned char scanline[128];
    long long start = stream.tellg();
    while (count--) {
        stream >> scanline[read++]; // <---- This is the only line which differs
    }
    long long end = stream.tellg();
    std::cout << end - start << "\n";
}

{
    std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
    unsigned char count = 128;
    unsigned char read = 0;
    unsigned char scanline[128];
    long long start = stream.tellg();
    while (count--) {
        stream.read((char*)&scanline[read++], 1); // <---- This is the only line which differs
    }
    long long end = stream.tellg();
    std::cout << end - start << "\n";
}

My problem is that the first version outputs 153 (probably dependent on input data) and the second one outputs 128 (which is what I expected). This must have something to do with how data is extracted in the first version but I fail to understand why it doesn't work. Shouldn't it just call:

istream& operator>> (istream& is, unsigned char& ch);

and move the filepos one byte each time?

0

2 Answers 2

10

If you read the description for operator>> (e.g. here), then you will see that it skips whitespace before reading, until it hits whitespace again. Whitespace is not only space (0x20) but also things like tab (0x09) and newline (0x0a).

So if your binary data contains bytes that are considered whitespace for a text file, then operator>> will read but not store them, which will skew the numbers reported by tellg.

4

It is not a good Idea to use the operator>> when you open a stream in binary.
I assume that in your case there is some sort of white space character(e.g 0x20) in your stream.
You could use the skipws modifier that those are also read.

1
  • It's not a question of whether you open the stream in binary or not; it's whether the stream contains binary data. (It's common to open text streams in binary, and handle the line ending conventions in your code.) May 29, 2012 at 8:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.