0

我需要从stdint.h在int16_t中存储值。如何从用户终端读取此值?

这个答案的方式(所以,我们有 int32_t、int16_t、uint64_t 等。但是 atoi32、atoi16、atoui64 等在哪里?)在 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。

4

1 回答 1

2

停止使用旧的 C 函数,并开始使用 C++ 功能:

std::string value = "111";

std::istringstream is(value);
if (is >> tmp)
    std::cout << "OK\n";

如果您想从用户那里读取它,请std::cin改用:

if (std::cin >> tmp)
    std::cout << "OK\n";
于 2013-05-29T17:54:18.500 回答