我有一个可以成功读取CSV文件的程序。所述CSV文件是由3列分隔的列表。每列之间有一个逗号。例如使用getLine在C++中读取CSV文件中的分隔列
线1 ---艺术家,流派,歌曲 线2 ---迈克尔·杰克逊,流行音乐,惊悚片
等。我想要我的节目要做的是读第一行,并把艺术家,流派和歌曲作为选项打印出来。例如
“选择选项” 1.艺术家 2.体裁 3.宋
,然后当他们选择一个选项,那么就说明他们要么所有的艺术家,歌曲,或CSV流派文件。
到目前为止,我有我的程序读取CSV并将每行放入向量中。这是到目前为止我的代码...
#include <iostream>
#include <sstream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream infile("music_list.csv");
string line = "";
vector<string> all_words;
cout << "Hello";
while (getline(infile, line))
{
stringstream strstr(line);
string word = "";
while (getline(strstr,word, ','))
{
all_words.push_back(word);
}
for (unsigned i = 0; i < all_words.size(); i++)
{
cout << all_words.at(i)<< "\n";
}
}
system("pause");
return 0;
}
我只是遇到问题搞清楚如何把它读的第一线,每根弦在已经用逗号分隔,并具有第一线分开然后作为选项输出给用户。因此,本质上,我可以在CSV文件中将艺术家,流派,歌曲更改为开胃菜,菜肴,饮料等。
相关:http://stackoverflow.com/q/236129/951890 – 2013-04-08 03:45:03