c ++读取csv文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19936483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ reading csv file
提问by programing_is_hard
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++ 教授、图书馆、John
then it reads like className/ classLocation/ Professor c++/ Library / John
然后它读起来像 className/classLocation/Professional 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
无论如何,我可以将最后一列与下一行的第一列分开吗?谢谢你,很抱歉,这令人困惑
回答by
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 文件的简化解析。
回答by xealits
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
和阅读文件的信息——请留下一些评论。)
回答by F?rid Alijani
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:
如果您有兴趣void
在您的main
部分中调用一个函数,请查看以下代码:
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