【发布时间】:2016-03-26 19:57:42
【问题描述】:
我一直在为整行(\n 或 \r)的事情而苦苦挣扎,我被分配了一项任务来读取其中包含 4K 行的 .csv 文件。怀着好奇心,我找到了阅读的方法csv 文件并将每个字段/单词与分隔符 ',' 分开。
std::istream& safeGetline(std::istream& is, std::string& t)
{
t.clear();
std::istream::sentry se(is);
std::streambuf* sb = is.rdbuf();
for(;;) {
int c = sb->sbumpc();
switch (c) {
case '\r':
if(sb->sgetc() == '\n')
sb->sbumpc();
return is;
case EOF:
// Also handle the case when the last line has no line ending
if(t.empty())
is.setstate(std::ios::eofbit);
return is;
default:
t += (char)c;
}
}
}
int main()
{
cout<<"Enter the file path :";
string filename;
cin>>filename;
ifstream file;
file.open(filename.c_str(),ios::in);
vector<string>arr;
string content;
string arr2;
stringstream ss;
// sqlite3 *db;int rc;sqlite3_stmt * stmt;
int i=0;
while (!safeGetline(file,content).eof())--here is the problem
{
ss<<content;
//since some of the field content falls next line i have decided to remove the '\n'
content.erase(std::remove(content.begin(), content.end(), '\n'), content.end());
while (getline(ss,arr2,','))
{
arr.push_back(arr2);
}
}
}
这里是while (!safeGetline(file,content).eof())--我认为这段代码会从 CSV 文件中读取第一行并通过 while (getline(ss,arr2,',')) 进行分隔符分隔,但发生的情况是 safeGetline() 以及正常的 getline() --我之前尝试过而不是safeGetline() 读取整个内容并通过分隔符分隔部分,这让我很难在数据库中插入这些字段
例如:
4xxxxxx,"field2",field3,,,,field7
400x1x2,"field2",,field4,,,field7
代码开始读取后,while(!safeGetline(file,content).eof())返回
输出:
4xxxxxx,"field2",field3,,,,field7400x1x2,"field2",,field4,,,field7
这里的 field7 和第二行的值 400x1x2 组合在一起field7400x1x2--当我将这些字段插入我的表中时,这会产生虚假结果(即)值在表中不正确地混乱。
那么在我的情况下,我如何才能真正执行逐行读取操作(即)读取->单独分隔符->推送到矢量->插入表格->第二次读取->.....
【问题讨论】:
标签: c++ sqlite csv c++11 getline