【问题标题】:Is there a way to directly read a number from file to unsigned char variable?有没有办法直接从文件中读取一个数字到 unsigned char 变量?
【发布时间】:2017-05-19 06:28:00
【问题描述】:

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

【问题讨论】:

  • 不,我不这么认为。
  • 这不能回答问题,但“将其转换为无符号字符”不是必需的; int 可转换为 unsigned char,因此不需要强制转换。

标签: c++ file char unsigned-char


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-24
    • 2013-06-25
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2021-04-14
    相关资源
    最近更新 更多