c ++读取csv文件 [英] c++ reading csv file

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

问题描述

我想使用c ++
读取csv文件,这里是我的代码

I want to read csv file by using c++ so here is my code

 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, Professor c++, Library, John

那么它就像className / classLocation / Professor c ++ /图书馆/ John

then it reads like 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

推荐答案

逐行读取文件:

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

行到istingstream并读取字段:

Pass each line to a istingstream and read the fields:

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

strong>免责声明:
这是对csv文件的简化解析。

Disclaimer: This is a simplified parsing of a csv-file.

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

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