简体   繁体   English

Getline函数未解析CSV文件中的行尾

[英]Getline function is not parsing end of line in CSV file

I am simply trying to read a CSV file and print it on the terminal, but I think the getLine() function is not parsing the end of line character. 我只是试图读取CSV文件并在终端上打印它,但是我认为getLine()函数不能解析行尾字符。 I thought that it was because the file that I was reading was created on Windows and I was running the script on Linux. 我以为是因为我正在读取的文件是在Windows上创建的,而我在Linux上运行了该脚本。 To test that theory, I created a new CSV file on Linux, but it is having the same issue. 为了验证该理论,我在Linux上创建了一个新的CSV文件,但存在相同的问题。

CSV File: CSV档案:

 julio,tito,monroy felipe,aguilar,jowell 
readCsv.open("test.csv", ios::in);
if(readCsv.is_open()){
    string time;
    string in;
    string out;
    int count = 1;

    while(!readCsv.eof()){
        getline(readCsv, time, ',');
        getline(readCsv, in, ',');
        getline(readCsv, out, ',');

        printf("%d : %s %s %s", count, time.c_str(), in.c_str(), out.c_str());

        count++;
    }

} else {
    printf("There was an error when trying to open the csv file. \n");
}

What am I doing wrong? 我究竟做错了什么?

Like this 像这样

while (getline(readCsv, time, ',') &&
        getline(readCsv, in, ',') &&
        getline(readCsv, out))
{

There are two things wrong with your version. 您的版本有两件事。 Firstly your third column is terminated by an end of line, not by a comma, so getline(readCsv, out, ',') is wrong. 首先,您的第三列以行尾而不是逗号结尾,因此getline(readCsv, out, ',')是错误的。 Secondly your understanding of how eof works is incorrect, see here 其次,您对eof工作原理的理解不正确,请参见此处

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

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