[英]Atoi function while reading a .csv file C++
The execution of my code crashes when it gets to the "atoi" function, but I cannot understand why. 到达“ atoi”功能时,我的代码执行崩溃,但我不明白为什么。 The code is supposed to read a matrix from a .csv file, considering: - the first row (so till the first '\\n') and saving each element (separated by a ',') in a vector of ints;
该代码应该从.csv文件中读取矩阵,考虑如下:-第一行(直到第一个'\\ n'),并将每个元素(以','分隔)保存为ints向量; - the rest of the matrix, by looking at each element and creating a specific object if the number read is 1 or 2. I don't get any exception while debugging the program, it just crashes during the execution (and using the system ("PAUSE") I could figure out it was the atoi function which didn't work properly).
-矩阵的其余部分,通过查看每个元素并在读取的数字为1或2的情况下创建特定的对象。在调试程序时我没有遇到任何异常,它只是在执行过程中崩溃(并使用系统( “ PAUSE”)我可以弄清楚这是atoi函数无法正常运行)。 Can you help me understand what is going wrong?
您能帮我了解问题出在哪里吗? Thank you very much.
非常感谢你。 Ps: I also attached all the libraries I'm loading... maybe it can help :)
附:我还附上了我正在加载的所有库...也许可以帮上忙:)
#include <fstream>
#include <stdio.h>
#include <sstream>
#define nullptr 0
#include <string>
#include "classi.h"
#include <cstdlib>
#include <stdlib.h>
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
ifstream file("problem.csv");
unsigned int N = 0;
unsigned int M = 0;
char c; //modificato char * c;
unsigned int i=0,j=0, k=0, n_iter, j_temp =0;
std::vector<car> v_row;
std::vector<car> v_col_temp;
std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //
std::string iterazioni; //location where I want to save the first row as a string and then cut it into pieces (iteraz) and then convert (atoi --> iter)
std::string iteraz;
while(!file.eof()){
std::getline(file,iterazioni,'\n');
stringstream it(iterazioni);
while (it.good()) {
std::getline(it,iteraz, ',');
iter[k] = atoi(iteraz.c_str());
if(iter[k]<0){
cout<<"Errore: negative #of iterations"<<endl;
break;
}
iter.push_back(0);
k++;
}
iter.pop_back();
file.get(c);
if (c=='1'){
blue_car b(i,j);
if (v_col_temp[i].get_next() != nullptr)
v_col_temp[i].insert_tail(&b);
else
v_col_temp[i].insert_head(&b);
}
if (c=='2'){
red_car r(i,j);
if (v_row[i].get_next() != nullptr)
v_row[i].insert_tail(&r);
else
v_row[i].insert_head(&r);
}
if (c==',') {
j++;
if (i == 0)
j_temp++;
}
if (c=='\n'){
car p;
v_row.push_back(p);
v_col_temp.push_back(p);
i++;
if (j != j_temp) {
std ::cout<<"errore input non valido: numero righe/colonne non coerente"<<endl;
}
j=0;
}
else if ((c!='\n') && (c!=',') && (c!='0') && (c!='1') && (c!='2'))
std ::cout<<"errore input non valido"<<endl;
};
n_iter = k-1;
M=i;
N=j+1;
...
Your program crashes because you failed to initialize the contents of the iter
vector. 您的程序崩溃是因为您未能初始化
iter
向量的内容。
std::vector<int> iter; // location where I want to save the ints corresponding to the elements of the first row of the .csv file //
You declare and construct this vector. 您声明并构造此向量。 The vector is empty at this point, and it has no elements.
该向量此时为空,并且没有任何元素。
At some point later: 稍后在某个时候:
iter[k] = atoi(iteraz.c_str());
The initial value of k
is 0, so this attempts to assign the return value from atoi()
to iter[0]
. k
的初始值为0,因此这会尝试将atoi()
的返回值分配给iter[0]
。
The problem is, of course, there is no iter[0]
. 当然,问题是没有
iter[0]
。 The iter
vector is still empty, at this point. 此时,
iter
向量仍然为空。
Additional comments, which is sadly true for at least 50% of these kinds of questions on stackoverflow.com: 附加评论,至少对于stackoverflow.com上至少50%的此类问题是正确的:
1) "using namespace std"; 1)“使用命名空间std”; is a bad practice, that should be avoided
这是一个坏习惯,应该避免
2) Do not use system("pause") as well , as you referenced in your question. 2)也不要使用您在问题中引用的system(“ pause”) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.