简体   繁体   English

c ++读取csv文件

[英]c++ reading csv file

I want to read csv file by using c++ so here is my code 我想用c ++读取csv文件,所以这是我的代码

 int main(){
 ifstream classFile("class.csv");
 vector<string> classData;

 while (getline(classFile, line,',')) // there is input overload classfile
        {
            classData.push_back(line);  

        }
}

here is my question : my problem is when it reads the last column of each row (since it is not separated by comma) it reads last column data and first of next row data for example if my data was like 这是我的问题:我的问题是当它读取每行的最后一列时(因为它没有用逗号分隔),它读取最后一列数据和下一行数据的第一列,例如,如果我的数据是

className, classLocation, Professor c++, Library, John className,classLocation,c ++教授,图书馆,约翰

then it reads like className/ classLocation/ Professor c++/ Library / John 然后它读起来像className / classLocation / Professor c ++ / Library / John

is there anyway that I can separate my last column from first of next row? 无论如何,我可以将我的最后一列与下一行中的第一列分开吗? Thank you and sorry that it is confusing 谢谢,抱歉这令人困惑

Read the file line by line: 逐行读取文件:

std::string line;
while(std::getline(stream, line)) ...

Pass each line to a istingstream and read the fields: 将每一行传递给istingstream并阅读字段:

std::istringstream s(line);
std::string field;
while (getline(s, field,',')) ...

Disclaimer : This is a simplified parsing of a csv-file. 免责声明 :这是对csv文件的简化解析。

Sorry, can I shove in some plain C into this thread? 对不起,我可以将一些简单的C推入这个帖子吗?

Reading csv is pretty clear there: 阅读csv非常清楚:

#include <stdio.h>


int main()
{
  float f1, f2;

  FILE *fp;
  fp = fopen("file.csv", "r");

  while (fscanf(fp, "%g,%g\n", &f1, &f2) == 2)
    printf("%g\n", f1+f2);
}

And quite certainly it should work where C++ works. 当然,它应该适用于C ++的工作原理。

There, in the while , we check how many objects fscanf has found: fscanf(fp, "%g,%g\\n", &f1, &f2) == 2 -- fscanf returns number of objects it finds. 在那里,在while ,我们检查的fscanf已经找到多少个对象: fscanf(fp, "%g,%g\\n", &f1, &f2) == 2 -的fscanf返回对象的数量发现。

I hope it might be of help to someone. 我希望它对某人有所帮助。

(And if anybody would like to see more info on fscanf and reading files -- leave some comments.) (如果有人想看到关于fscanf和阅读文件的更多信息 - 留下一些评论。)

In case you are interested to have a void function to be called in your main section, please take a look at the following code: 如果您有兴趣在main部分中调用void函数,请查看以下代码:

void readCSV(const string &strPath2Dataset)
{   
    ifstream csvFile;
    string strPathCSVFile = strPath2Dataset + "/test.csv";
    csvFile.open(strPathCSVFile.c_str());

    if (!csvFile.is_open())
    {
        cout << "Path Wrong!!!!" << endl;
        exit(EXIT_FAILURE);
    }

    vector<long double> timeStampIMU;
    vector<long double> gyro_X;
    vector<long double> gyro_Y;
    vector<long double> gyro_Z;

    vector<long double> acc_X;
    vector<long double> acc_Y;
    vector<long double> acc_Z;

    string line;
    vector <string> vec;
    getline(csvFile, line); // skip the 1st line

    while (getline(csvFile,line))
    {
        if (line.empty()) // skip empty lines:
        {
            //cout << "empty line!" << endl;
            continue;
        }

        istringstream iss(line);
        string lineStream;
        string::size_type sz;

        vector <long double> row;

        while (getline(iss, lineStream, ','))
        {  
            row.push_back(stold(lineStream,&sz)); // convert to double
        }

        timeStampIMU.push_back(row[0]);

        gyro_X.push_back(row[1]);
        gyro_Y.push_back(row[2]);
        gyro_Z.push_back(row[3]);

        acc_X.push_back(row[4]);
        acc_Y.push_back(row[5]);
        acc_Z.push_back(row[6]);
    }

    //cout << "size ts = " << timeStampIMU.size() << endl;
    for (size_t i = 0; i < timeStampIMU.size(); i++)
    {
        cout << "ts_imu = " << setprecision(12) << timeStampIMU[i] << endl;

        cout << "gx = " << setprecision(12) << gyro_X[i] << endl;
        cout << "gy = " << setprecision(12) << gyro_Y[i] << endl;
        cout << "gz = " << setprecision(12) << gyro_Z[i] << endl;

        cout << "ax = " << setprecision(12) << acc_X[i] << endl;
        cout << "ay = " << setprecision(12) << acc_Y[i] << endl;
        cout << "az = " << setprecision(12) << acc_Z[i] << endl;
        cout << "--------------------------------" << endl;
    }
}

My .csv file is dataset provided from IMU sensor, separated by comma: 我的.csv文件是IMU传感器提供的数据集,以逗号分隔:

TimeStamp, Gyro_X, Gyro_Y, Gyro_Z, Acc_X, Acc_Y, Acc_Z

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM