c++读int16_t (c++编译器)

C++ read int16_t (G++ compiler)

本文关键字:c++ 编译器 int16      更新时间:2023-10-16

我需要从 stint .hint16_t中存储值。我如何从用户终端读取这个值?

从这个答案的方式(所以,我们有int32_t, int16_t, uint64_t等。但是atoui32, atoui16, 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

停止使用旧的C函数,开始使用c++函数:

std::string value = "111";
std::istringstream is(value);
if (is >> tmp)
    std::cout << "OKn";

如果您想从用户那里读取它,那么使用std::cin代替:

if (std::cin >> tmp)
    std::cout << "OKn";