void read_file_into_2d_array(const string file_name, int row, int col)
{
float data[row][col];
std::ifstream file(file_name);
for (int i = 0; i < row; ++i)
{
std::string line;
std::getline(file, line);
if(!file.good())
break;
std::stringstream iss(line);
for (int j = 0; j < col; ++j)
{
std::string val;
std::getline(iss, val, ' ');
if (!iss.good())
break;
std::stringstream convertor(val);
convertor >> data[row][col];
}
}
}
int main()
{
string file_name = "arr.txt";
read_file_into_2d_array(file_name, 3, 4);
return 0;
}
corrupted double-linked list, Aborted(core dumped)
我成功地构建了我的项目,但是当我运行它时,它在 Ubuntu 14.04 上引发错误“损坏的双链表,中止(核心转储)”。而它在 Windows 7 上运行良好。Ubuntu 上的编译器是 g++,在 Window 上是 mingw32-g++.exe。不知道是什么问题?
最佳答案
在行中进行越界访问很有可能导致崩溃
convertor >> data[row][col];
试试这个:
convertor >> data[i][j];
关于c++ - fstream 文件 ("arr.txt") 在 Ubuntu14.04 上引发错误 "corrupted double-linked list, Aborted"但在 Windows 7 上运行良好,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966076/