【发布时间】:2013-05-29 17:51:34
【问题描述】:
我需要在 stdint.h 的 int16_t 中存储价值。如何从用户终端读取此值?
这个答案 (So, we have int32_t, int16_t, uint64_t, etc.. But where are the atoi32, atoi16, atoui64, etc...?) 的方式不适用于 Ubuntu g++ 编译器。
我更喜欢使用标准 C++ 库。比如:
#include <cstdio>
#include <stdint.h>
#include <iostream>
using namespace std;
int main ( void ) {
char value [] = "111";
int16_t tmp;
if ( sscanf ( value, "%???", & tmp) == 1 ) cout << "OK" << endl;
return 0;
}
或者是更好地读取标准整数然后转换它?
我不使用 C++11。
【问题讨论】:
-
您使用的是 C++11 吗?如果是这种情况,您需要在字符串文字和标识符之间添加一个空格:
scanf( "%" SCNd16, &tmp);,否则它会将其解释为后缀。 -
对不起,我忘记写了。我不使用 C++11,但谢谢。