无符号char类型变量I/O失败

I/O Fail of unsigned char typed variable

本文关键字:失败 char 类型变量 无符号      更新时间:2023-10-16

我有一个名为input.in的文件。我需要读取数字并将其存储在一个无符号char类型的变量中。我读入数字并在屏幕上打印变量的内容,我可以看到它是真的(打印1)。然而,if语句失败了。它向标准输出输出输出False。为什么会发生这种情况?如何将此数字存储到无符号字符变量中?

输入。在文件:

asus@ubuntu:~/Desktop$ cat input.in 
1

我在Ubuntu工作。sizeof (unsigned char) = 1

#include <fstream>
#include <iostream>
using namespace std;
int main(){
    unsigned char ucbuffer;
    fstream in;
    in.open("input.in",ios::binary | ios::in);
    in.read((char*)&ucbuffer,1);
    cout << ucbuffer << endl;
    if (ucbuffer==1) cout << "True" << endl;
    else cout << "False" << endl;
        return 0;
}

if (ucbuffer==1)为假。取值为"'1'",不为"1"。'1'数值为49