c++ 分段在 .csv 文件中读取失败,memmove-vec-unaligned-erms.S:没有这样的文件或目录
c++ segmentation failed read in .csv file, memmove-vec-unaligned-erms.S: No such file or directory
我的代码应该从参数中给出的“.csv”文件读入二维向量 table。每次我尝试 运行 它都会说“分段错误(核心已转储)。我什至试图在控制台中用 gdb(g++ 调试器)修复它。我最接近问题的是这条消息:
Program received signal SIGSEGV, Segmentation fault.
__memmove_sse2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:314
314 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such
file or directory.
我不知道应该怎么做才能解决它。
void Table::read_file(std::string f_str) {
std::fstream file;
std::string line;
file.open(f_str, std::ios::in);
if (!file) { incorrect_input(); }
else {
while (std::getline(file, line)) // first while to resize our 2d vector table
{
std::istringstream iss(line);
std::string result;
int cols = 1;
while (std::getline(iss, result, sep))
{
cols++;
}
if (getColumn() < cols) { setColumn(cols); }
setRow(getRow() + 1);
}
file.clear();
file.seekg(0, std::ios_base::beg);
int i = 0;
int j = 0;
while (std::getline(file, line)) // second while to get the records segmented in to our 2d vector table 'cells'
{
std::istringstream iss(line);
std::string result;
while (std::getline(iss, result, sep))
{
cellcontainer[i][j] = result;
j += 1;
}
i += 1;
}
}
file.close();
}
更新:
所以 cellcontainer 是一个二维向量 table/matrix。它由一个 header 文件构建,主要在代码开头调用它,在通过参数给出“.csv”文件之后。它包含二维向量的“单元格”table。在 header:
中看起来像这样
class Table {
private:
std::vector<std::vector<std::string>> cellcontainer;
int row;
int column;
char sep = ';';
public:
Table() : row(1), column(1) {
cellcontainer.push_back(std::vector<std::string>());
cellcontainer[0].push_back("-");
}
update_2:
我重写了这一行 cellcontainer[i][j] = result;
对此cellcontainer.at(i).at(j) = result;
它说 std::out_of_range
感谢大家,但特别感谢 Raymond Chen。
解决方案是: 在我的代码的第一个 while() 中,我只更新了 class 变量的列和行,甚至没有更改大小我的二维向量容器 (cellcontainer)。
现在看起来像这样:
while (std::getline(iss, result, sep))
{
addColumn(1); // <-- addColumn() function arg is an int and it adds to our cellcontainer with resizing.
cols++;
}
if (getColumn() < cols) { setColumn(cols); }
setRow(getRow() + 1);
addRow(1); // <-- addRow() function arg is an int and it adds to our cellcontainer with resizing
我的代码应该从参数中给出的“.csv”文件读入二维向量 table。每次我尝试 运行 它都会说“分段错误(核心已转储)。我什至试图在控制台中用 gdb(g++ 调试器)修复它。我最接近问题的是这条消息:
Program received signal SIGSEGV, Segmentation fault. __memmove_sse2_unaligned_erms () at ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S:314 314 ../sysdeps/x86_64/multiarch/memmove-vec-unaligned-erms.S: No such file or directory.
我不知道应该怎么做才能解决它。
void Table::read_file(std::string f_str) {
std::fstream file;
std::string line;
file.open(f_str, std::ios::in);
if (!file) { incorrect_input(); }
else {
while (std::getline(file, line)) // first while to resize our 2d vector table
{
std::istringstream iss(line);
std::string result;
int cols = 1;
while (std::getline(iss, result, sep))
{
cols++;
}
if (getColumn() < cols) { setColumn(cols); }
setRow(getRow() + 1);
}
file.clear();
file.seekg(0, std::ios_base::beg);
int i = 0;
int j = 0;
while (std::getline(file, line)) // second while to get the records segmented in to our 2d vector table 'cells'
{
std::istringstream iss(line);
std::string result;
while (std::getline(iss, result, sep))
{
cellcontainer[i][j] = result;
j += 1;
}
i += 1;
}
}
file.close();
}
更新: 所以 cellcontainer 是一个二维向量 table/matrix。它由一个 header 文件构建,主要在代码开头调用它,在通过参数给出“.csv”文件之后。它包含二维向量的“单元格”table。在 header:
中看起来像这样class Table {
private:
std::vector<std::vector<std::string>> cellcontainer;
int row;
int column;
char sep = ';';
public:
Table() : row(1), column(1) {
cellcontainer.push_back(std::vector<std::string>());
cellcontainer[0].push_back("-");
}
update_2:
我重写了这一行 cellcontainer[i][j] = result;
对此cellcontainer.at(i).at(j) = result;
它说 std::out_of_range
感谢大家,但特别感谢 Raymond Chen。
解决方案是: 在我的代码的第一个 while() 中,我只更新了 class 变量的列和行,甚至没有更改大小我的二维向量容器 (cellcontainer)。
现在看起来像这样:
while (std::getline(iss, result, sep))
{
addColumn(1); // <-- addColumn() function arg is an int and it adds to our cellcontainer with resizing.
cols++;
}
if (getColumn() < cols) { setColumn(cols); }
setRow(getRow() + 1);
addRow(1); // <-- addRow() function arg is an int and it adds to our cellcontainer with resizing