有没有办法直接从文件中读取一个数字到unsigned char变量?

时间:2017-01-04 20:31:42

标签: c++ file char unsigned-char

如在文本文件中读取“117”并给出变量值'u',而不是'1'。而且我知道我可以通过将值读取到int然后将其转换为unsigned char来简单快速地完成它,我只需要知道我是否可以直接执行它。

1 个答案:

答案 0 :(得分:2)

尝试scanf格式说明符hhud(参见scanf format specifiers at cplusplus.com

unsigned char c;
scanf("%hhud", &c);

在终端中,我输入117,变量c然后在调试器中显示值'u' ...