【发布时间】:2016-02-10 04:55:24
【问题描述】:
所以我试图使用 c++ 读取一个 csv 文件并进行一些计算并输出到另一个 csv 文件。 一切正常,但是当程序读取一行时:
<a href="http://www.google.com" target="_blank">google</a>
我想看看程序读到了什么,所以我计算出那个字符串,它显示:
<a href=""http://www.google.com"" target=""_blank"">google</a>
基本上每个双引号都会加倍? 我该如何解决这个问题?
编辑:
这是我的代码:
int main()
{
ifstream read;
ofstream write;
string line;
string cell;
int col = 0;
string temp;
string links;
read.open("Book1.csv");
write.open("output.csv");
if (read.is_open())
{
cout << "opened" <<endl ;
getline(read, line);
while(getline(read,temp))
{
stringstream line(temp);
while (getline(line, cell, ','))
{
if (col > 9)
{
links.pop_back();
write << links<<endl;
col = 0;
links = "";
break;
}
else
{
if (cell != "")
{
if (col == 0)
{
write << cell<<',';
}
else if (col == 1)
{
write << cell<<',';
}
else
{
cell.erase(0, 1);
cell.pop_back();
links += cell;
links += '/';
}
cout << cell << endl;
}
col += 1;
}
}
}
}
else
{
cout << "failed" << endl;
}
read.close();
write.close();
}
【问题讨论】:
-
你是如何读取文件的。使用标准库调用不会“偶然”发生这种情况。例如,请参阅:ideone.com/j3jJrO。
-
@Chad 我使用了 getline 和 stringstream。哦,顺便说一句,我怎样才能在 cmets 中制作那些灰色背景的?我对 SO 有点陌生
-
无法复制:ideone.com/SX4272
-
@PaulMcKenzie 我实际上是从 csv 文件中读取的
-
@andyz 所以给我们一行数据。与我的示例的唯一区别是“文件”是
std::cin。
标签: c++ csv double-quotes