【发布时间】:2019-06-30 15:20:21
【问题描述】:
我正在尝试读取包含 3 个人/患者行的 CSV 文件,其中 col 1 是用户 ID,col 2 是 fname,col 3 是 lname,col 4 是保险,col 5 是看起来的版本如下所示。
编辑:抱歉,我只是在这里复制/粘贴了我的 CSV 电子表格,所以它之前没有显示逗号。它看起来不是更像下面吗?下面的约翰还指出,版本后没有逗号,这似乎解决了这个问题!非常感谢约翰! (试图弄清楚我怎样才能接受你的回答:))
nm92,Nate,Matthews,Aetna,1
sc91,Steve,Combs,Cigna,2
ml94,Morgan,Lands,BCBS,3
我正在尝试在循环中使用 getline() 来读取所有内容,并且它在第一次迭代时工作正常,但 getline() 似乎导致它在下一次迭代中跳过一个值。知道如何解决这个问题吗?
我也不确定为什么输出如下所示,因为我没有看到代码中的“sc91”和“ml94”行打印在哪里。这就是当前代码的输出。
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++ 字符串数组。
#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;
}
}
【问题讨论】:
-
吹毛求疵,这不是 CSV - CSV 代表“逗号分隔值”,并且该数据中没有逗号。
-
getline 在 cin 或文件上的工作方式没有区别(如果有的话会很奇怪)。不幸的是,您没有引用您尝试读取的实际数据,因为您显示的数据中根本没有逗号,但我猜您的问题是 版本后没有逗号,所以
getline(inputFile,sversion,',');应该是getline(inputFile,sversion);。正如重复指出的那样,while(!inputFile.eof())也是不正确的,但我认为这不是您描述的问题的原因。 -
我建议您使用要读取的实际数据更新问题。如果我的猜测是正确的,我将重新提出问题,我们都会很高兴。
-
更新证明了@john 的怀疑。重新打开。
-
版本以
\n结尾,而不是,。您必须将getline(inputFile,sversion,',');替换为getline(inputFile,sversion);。仔细阅读这里的第一段cplusplus.com/reference/string/string/getline