C ++在循环内使用getline()读取CSV文件 [英] C++ Using getline() inside loop to read in CSV file

查看:321
本文介绍了C ++在循环内使用getline()读取CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取一个CSV文件,该文件包含3个人/患者的行,其中col 1是用户ID,col 2是fname,col 3是lname,col 4是保险,col 5是看起来是的版本像下面这样.

I'm trying to read in a CSV file that contains rows of 3 people/patients, where col 1 is userid, col 2 is fname, col 3 is lname, col 4 is insurance, and col 5 is version that looks something like below.

抱歉,我只是在这里复制/粘贴了CSV电子表格,因此以前没有显示逗号.它看起来不像下面吗?下面的约翰还指出,该版本之后没有逗号,这似乎可以解决该问题!非常感谢约翰! (试图弄清楚我如何接受您的回答:))

Apologies, I simply copy/pasted my CSV spreadsheet in here, so it didn't show the commas before. Wouldn't it look something more like below? John below also pointed out that there are no commas after the version, and this seemed to fix the issue! Thanks so much John! ( trying to figure out how I can accept your answer :) )

nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3

我正在尝试在循环内使用getline()读取所有内容,并且在第一次迭代中工作正常,但是getline()似乎导致它在下一次迭代中跳过了一个值.知道我该如何解决吗?

I'm trying to use getline() inside of a loop to read everything in, and it works fine for the first iteration, but getline() seems to be causing it to skip a value on the next iterations. Any idea how I can solve this?

我也不确定为什么输出如下所示,因为我看不到代码中有w/"sc91"和"ml94"的行.这就是当前代码的输出.

I'm also not sure why the output looks like below, because I'm not seeing where the lines w/ "sc91" and "ml94" are being printed in the code. This is what the output of the current code looks like.

userid is: nm92
fname is: Nate
lname is: Matthews
insurance is: Aetna
version is: 1
sc91
userid is: Steve
fname is: Combs
lname is: Cigna
insurance is: 2
ml94
version is: Morgan
userid is: Lands
fname is: BCBS
lname is: 3

insurance is:
version is:

我已经对getline()和>>流运算符之间的差异进行了大量研究,但是大多数getline()材料似乎都是围绕从cin获取输入,而不是从像这样的文件中读取,因此我在想w/getline()上正在发生什么,以及它是如何读取我不理解的文件的.不幸的是,当我尝试>>运算符时,这迫使我使用strtok()函数,并且我在使用c字符串并将其分配给C ++字符串数组时遇到了很多麻烦.

I've done a ton of research on differences between getline() and the >> stream operator, but most of the getline() materials seem to revolve around getting input from cin rather than reading from a file like here, so I'm thinking there's something going on w/ getline() and how it's reading the file that I'm not understanding. Unfortunately when I tried >> operator, that forces me to use the strtok() function, and I was struggling a lot with c strings and assigning them to an array of C++ strings.

#include <iostream>
#include <string>                               // for strings
#include <cstring>                              // for strtok()
#include <fstream>                              // for file streams

using namespace std;

struct enrollee
{
    string userid = "";
    string fname = "";
    string lname = "";
    string insurance = "";
    string version = "";
};

int main()
{
    const int ENROLL_SIZE = 1000;               // used const instead of #define since the performance diff is negligible,
    const int numCols = 5;                    // while const allows for greater utility/debugging bc it is known to the compiler ,
                                                // while #define is a preprocessor directive
    ifstream inputFile;                         // create input file stream for reading only
    struct enrollee enrollArray[ENROLL_SIZE];   // array of structs to store each enrollee and their respective data
    int arrayPos = 0;

    // open the input file to read
    inputFile.open("input.csv");
    // read the file until we reach the end
    while(!inputFile.eof())
    {
        //string inputBuffer;                         // buffer to store input, which will hold an entire excel row w/ cells delimited by commas
                                                    // must be a c string since strtok() only takes c string as input
        string tokensArray[numCols];
        string userid = "";
        string fname = "";
        string lname = "";
        string insurance = "";
        string sversion = "";
        //int version = -1;

        //getline(inputFile,inputBuffer,',');
        //cout << inputBuffer << endl;

        getline(inputFile,userid,',');
        getline(inputFile,fname,',');
        getline(inputFile,lname,',');
        getline(inputFile,insurance,',');
        getline(inputFile,sversion,',');

        enrollArray[0].userid = userid;
        enrollArray[0].fname = fname;
        enrollArray[0].lname = lname;
        enrollArray[0].insurance = insurance;
        enrollArray[0].version = sversion;

        cout << "userid is: " << enrollArray[0].userid << endl;
        cout << "fname is: " << enrollArray[0].fname << endl;
        cout << "lname is: " << enrollArray[0].lname << endl;
        cout << "insurance is: " << enrollArray[0].insurance << endl;
        cout << "version is: " << enrollArray[0].version << endl;
    }
}

推荐答案

您的问题是每行最后一个数据项后面没有逗号,因此

Your problem is that there is no comma after the final data item in each line, so

 getline(inputFile,sversion,',');

是不正确的,因为它会读取下一个逗号,该逗号实际上位于下一位患者的用户ID之后的下一行.这就解释了您看到的输出,在该输出中,下一个专利的用户ID在哪里获得了该版本的输出.

is incorrect because it reads to the next comma, which is actually on the next line after the user id of the next patient. This explains the output you see where the user id of the next patent gets output with the version.

要解决此问题,只需将上面的代码替换为

To fix this simply replace the code above with

 getline(inputFile,sversion);

将根据需要读取到行尾.

which will read to the end of line as required.

这篇关于C ++在循环内使用getline()读取CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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