将字符串(以char*形式给出)解析为int的C++方式是什么?强大而清晰的错误处理是一个优点(而不是返回零).
我想转换string为char数组但不是char*.我知道如何将字符串转换为char*(通过使用malloc或我在代码中发布它的方式) - 但这不是我想要的.我只是想转换string为char[size]数组.可能吗?
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
int main()
{
// char to string
char tab[4];
tab[0] = 'c';
tab[1] = 'a';
tab[2] = 't';
tab[3] = '\0';
string tmp(tab);
cout << tmp << "\n";
// string to char* - but thats not what I want
char *c = const_cast<char*>(tmp.c_str());
cout << c << "\n";
//string to char
char tab2[1024];
// …Run Code Online (Sandbox Code Playgroud) 我正在尝试将std::string存储的a std::vector转换为整数并将其作为参数传递给函数.
这是我的代码的简化版本:
vector <string> record;
functiontest(atoi(record[i].c_str));
Run Code Online (Sandbox Code Playgroud)
我的错误如下:
error: argument of type ‘const char* (std::basic_string<char, std::char_traits<char>, std::allocator<char> >::)()const’ does not match ‘const char*’
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
Cppcheck在这样的代码中发现了一个潜在的问题:
float a, b, c;
int count = sscanf(data, "%f,%f,%f", &a, &b, &c);
Run Code Online (Sandbox Code Playgroud)
它说:"没有字段宽度限制的scanf可能会因大量数据而崩溃".怎么可能?这是某些sscanf实现中的已知错误吗?我知道数字可能会溢出(数字),但程序怎么会崩溃?这在cppcheck中是误报吗?
我发现了一个类似的问题:scanf Cppcheck警告,但答案并不完全令人满意.答案提到了类型安全,但这不应该是一个问题.
Cppcheck为scanf显示以下警告:
Message: scanf without field width limits can crash with huge input data. To fix this error message add a field width specifier:
%s => %20s
%i => %3i
Sample program that can crash:
#include
int main()
{
int a;
scanf("%i", &a);
return 0;
}
To make it crash:
perl -e 'print "5"x2100000' | ./a.out
我无法崩溃这个程序输入"巨大的输入数据".我应该输入什么才能让这次崩溃?我也不明白这个警告中最后一行的含义:
perl -e ...
我真的很困惑.我必须遗漏一些相当简单的东西,但我读到的关于strtol()的任何内容都没有意义.有人能以一种非常基本的方式为我拼出它,并举例说明我可能会得到类似下面的东西吗?
string input = getUserInput;
int numberinput = strtol(input,?,?);
Run Code Online (Sandbox Code Playgroud) 我有一个程序,它逐行读取文件的内容,将每一行存储到字符串向量中,然后打印向量的内容。
\n\n将文件数据读入字符串向量后,我尝试将每一行string从uint32. 文件的每一行都由32-bit数字组成。输入数据文件的示例(input_file.dat):
31401402\n67662718\n74620743\n54690001\n14530874\n13263047\n84662943\n09732679\n13839873\nRun Code Online (Sandbox Code Playgroud)\n\n我想将这些字符串中的每一个转换为uint32_t, 对于我编写的另一个程序,该程序将这些数字转换为 ASCII 格式(该程序需要用于uint32_t转换)。
到目前为止我的计划:
\n\n#include <iostream>\n#include <fstream>\n#include <string>\n#include <vector>\n\n/*\n * Program to iterate through all lines in file and put them in given vector\n *\n */\nbool getFileContent(std::string fileName, std::vector<std::string> & vecOfStrs)\n{\n\n // Open the File\n std::ifstream in(fileName.c_str());\n\n // Check if object is valid\n if(!in)\n {\n std::cerr << "Cannot open the File : "<<fileName<<std::endl;\n return false;\n }\n\n std::string …Run Code Online (Sandbox Code Playgroud) 我试图将我从文件中读入的字符串转换为int值,以便将其存储在整数变量中.这就是我的代码:
ifstream sin;
sin.open("movie_output.txt");
string line;
getline(sin,line);
myMovie.setYear(atoi(line));
Run Code Online (Sandbox Code Playgroud)
在这里,setYear是Movie类中的一个mutator(myMovie是Movie类的一个对象),如下所示:
void Movie::setYear(unsigned int year)
{
year_ = year;
}
Run Code Online (Sandbox Code Playgroud)
当我运行代码时,我收到以下错误:
error C2664: 'atoi' : cannot convert parameter 1 from 'std::string' to 'const char *'
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
Run Code Online (Sandbox Code Playgroud)