简体   繁体   中英

ifstream very strange behaviour when attempting to read unsigned char between 9 and 13

I've been banging my head against the wall trying to understand this. I didn't find anything online, so I'm sharing it here in case someone can enlighten me.

#include <iostream>
#include <fstream>

int main(){
    std::ifstream in("bf", std::ios::binary);
    if (!in)
        return false;

    unsigned char byte;    
    while (!in.eof()){
        in >> byte;
        if (in.fail()){
            break;
        }
        std::cout<<(int)byte<<std::endl; 
    }
}

Running the above code where "bf" is a file produced by this python script:

f = open('bf','wb')
arr = bytearray([i for i in range(20)])
f.write(arr)
f.close()

Produces this output:

0
1
2
3
4
5
6
7
8
14
15
16
17
18
19

I'm running Ubuntu 18.04. Can anyone replicate the behaviour? Does anyone know what's happening?

The Python script is creating a binary file whose content is bytes 0..19. Your C++ code is then reading that file in binary mode and outputting the numeric value of the bytes it reads. The problem is it is not reading what you are expecting.

There are two problems with your C++ code:

  • you are using eof() incorrectly .

  • operator>> performs a formatted read , which is not what you want in this situation. operator>> ignores whitespace, and bytes 9..13 represent whitespace characters, which is why you don't see them being output. You need an unformatted read instead, such as by the steam's get() method

Try this:

#include <iostream>
#include <fstream>

int main(){
    std::ifstream in("bf", std::ios::binary);
    if (!in)
        return false;

    char byte;
    while (in.get(byte)){
        std::cout << (unsigned int)byte << std::endl;
    }
}

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM