My code should read in from ".csv" file given in the arguments, to a 2D vector table. Everytime I tried to run it it said "Segmentation fault (core dumped). I even tried to get it fiexed with gdb (g debugger) in console. The closest I've got to the promblem is this message:
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.
I don't know what should I do to fix it.
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();
}
udpate: So the cellcontainer is a 2D vector table/matrix. It's constructed by a header file, main calls for it at the start of the code, after the ".csv" file is given through arguments. It contains the "cells" of the 2D vector table. And it looks like this in 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:
I rewrote this line cellcontainer[i][j] = result;
To this cellcontainer.at(i).at(j) = result;
And it says std::out_of_range
CodePudding user response:
Thankfully for everyone but specially for Raymond Chen.
solution was: At the first while() of my code I only kept the columns and rows of the class variables updated and did not even changed the size of my container of the 2D vectors (cellcontainer).
Now it looks like this:
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