我正在尝试做一个套接字服务器,但是当我尝试传递数字时,我得到了 std::invalid_argument
并且我不知道为什么。我重写了代码以使其更简单,这里是:
std::string a = std::to_string(32);
char texto[5];
strcat(texto,a.c_str());
printf("%s", texto);//Isn't printing a string that you can convert to a number
std::string b = texto;
int s = std::stoi(texto);
std::cout << s;
我期望输出 32,但我收到了错误。
最佳答案
char texto[5];
strcat(texto,a.c_str());
texto
未初始化,因此它包含垃圾值。 strcat
必须首先阅读它们以寻找 NUL 终止符,从而为您提供未定义的行为。
您可能正在寻找 strcpy
而不是 strcat
。
关于c++ - 如何修复 std::stoi 中的 "std::invalid_argument",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56990627/