Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between istream::read and istream::operator>>

Tags:

c++

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?

like image 898
Andreas Brinck Avatar asked Dec 09 '25 06:12

Andreas Brinck


1 Answers

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.

like image 139
Some programmer dude Avatar answered Dec 11 '25 21:12

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!