c++ (VS2012) stdio:不能读取一个字节的文件,如果字节是09h

C++ (VS2012) stdio: Can't read one-byte file if that byte is 09h

本文关键字:字节 文件 09h 一个 如果 stdio VS2012 不能 不能读 c++ 读取      更新时间:2023-10-16

代码很简单。

unsigned char a_byte;
ifstream a_file("C:/file.bin", ios_base::binary);
if (a_file.is_open() && a_file.good())
{
    a_file.seekg(0);
    a_file >> a_byte;
    a_file.close();
}

问题是它不会从一个单字节文件中读取09h -我只是在a_byte var中得到0 .它确实可以处理不同的值。原因是什么?

流类的operator>>将在读入目标变量之前跳过空白。这里的字符值09h是TAB,它被计算为空白并被跳过。

如果你想读取每一个字符,试试get函数