在C ++中读取csv文件中的特定行 [英] Reading a particular line in a csv file in C++

查看:367
本文介绍了在C ++中读取csv文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个csv文件,其中包含许多行数据。我的函数传递lineNum作为参数。所以当用户输入4作为lineNum我想读取csv文件中的第4行。
我认为一个很好的方法来处理,这将是寻找\\\
和计数,停止当计数为lineNum-1,然后继续阅读下一行。
我认为这是做这个的体面的方式,但我完全困惑与实现。将会有一些帮助
这是我的代码

I have a csv file which contains many rows of data. my function passes lineNum as an argument. So when a user enters 4 as lineNum I want to read the 4th line in the csv file. I thought a good way to go through with this would be to look for \n 's and count them,stop when the count is lineNum-1, and then proceed to read the next line. I think this is decent way of doing this, but I am thoroughly confused with the implementation. would love some help Here is my code

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. ");
}


推荐答案

在CSV中的某一行,然后从该行读取逗号分隔的项目,然后这可能会有所帮助。我同意@ sanjaya-r,你应该保持简单。

If you just need to read a certain line in a CSV and then from that line read the comma separated items then this might help. I agree with @sanjaya-r that you should keep it simple.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;

int main (int argc, const char * argv[]) {
    string line, csvItem;
    ifstream myfile ("/tmp/file.csv");
    int lineNumber = 0;
    int lineNumberSought = 3;  // you may get it as argument
    if (myfile.is_open()) {
        while (getline(myfile,line)) {
            lineNumber++;
            if(lineNumber == lineNumberSought) {
                cout << line << endl; ;
                istringstream myline(line);
                while(getline(myline, csvItem, ',')) {
                    cout << csvItem << endl;
                }
            }
        }
        myfile.close();
    }
    return 0;
}

这篇关于在C ++中读取csv文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆