我写了一个函数,用于从包含ASCII十进制数的文件中读取并将它们转换为存储在int数组中的整数。以下是该函数代码:
void readf1()
{
int myintArray[100];
int i = 0;
int result;
string line = "";
ifstream myfile;
myfile.open("f1.txt");
if(myfile.is_open()){
//while not end of file
while(!myfile.eof()){
//get the line
getline(myfile, line);
/* PROBLEM HERE */
result = atoi(line);
myintArray[i] = result;
//myintArray[i]
cout<<"Read in the number: "<<myintArray[i]<<"\n\n";
i++;
}
}
}
问题在于atoi函数无法正常工作。我遇到的错误是cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int atoi(const char*)'
。我不确定为什么它不能正常工作,因为我查看了示例,并且我正在完全相同的方式使用它。有人知道我可能做错了什么吗?
cout << line
并发布它是什么吗? - David says Reinstate Monica