我有一个包含多行数据的 csv 文件。我的函数将 lineNum 作为参数传递。因此,当用户输入 4 作为 lineNum 时,我想读取 csv 文件中的第 4 行。我认为解决此问题的一个好方法是查找 \n 并计算它们,当计数为 lineNum-1 时停止,然后继续阅读下一行。我认为这是一种不错的方法,但是我对实现完全感到困惑。希望得到一些帮助这是我的代码
void ReadCsv( int lineNum){
ifstream inFile ("/media/LOGGING/darsv1.csv");
string line;
string dataArray[226900];
int i = 0;
int endofline =0;
int a, b, c, d, e;
while (getline (inFile, line)) {
//printf(line.c_str());
istringstream linestream(line);
string item ="";
int itemnum = 0;
if (lineNum==1) {
printf(" line number is 1. ");
while (getline (linestream, item, ',')) {
itemnum++;
dataArray[i]=item;
i++;
}
}
else {
while (getline (linestream, item,'\n')) {
endofline=endofline+1;
cout<<" went through line number "<<endofline<<" ";
printf(" inside inner while, looking for line. ");
if (endofline == lineNum-1) {
printf(" found the correct line. ");
while (getline (linestream, item, ',')) {
itemnum++;
dataArray[i]=item;
i++;
printf(" found the correct data in the line. ");
}
}
}printf(" out of inner while. ");
}printf(" out of outer if. ");
}
printf(" out of all while loops. ");
}