如何解决如何将csv中的单词分配给变量?
我有一个读取以下文件的代码: word1,word2,word,3 ....,word8 word1,word2,word,3 ....,word8 word1,word2,word,3 ....,word8
ifstream infile;
//string path;s
infile.open("file.csv");
string line;
CourseNode *current = head;
int i=1;
if(infile.is_open()){
while(getline(infile,line)) {
if(i == 1)
cout << "1st line==> "+line << endl; // ignore first line
else { // read data
string firstname,lastname,id,a,b,c,d,t ;
stringstream sst(line);
getline(getline (sst,firstname,','),');
//getline(getline(getline (sst,');
cout << "result==> "<< firstname<<" "<<lastname << endl;
}
i++;
}
我假设我必须使用此行并在其中插入我的字符串变量,但是我不确定如何! getline(getline(sst,firstname,','),lastname,',');
任何帮助将不胜感激!谢谢!
解决方法
要读取.csv
文件,您可以阅读整行,然后拆分以轻松加载数据:
std::ifstream infile;
infile.open("file.csv");
std::string line;
int i = 1;
if(infile.is_open()){
while(getline(infile,line)) {
if(i == 1) {
// ignore line
continue;
} else { // read data
std::vector<std::string> value; // this is where you stock the data
value = split(line); // add your implementation of split
// here: do something with the data in value
}
i++;
}
}
您value
文件中一行中的每个数据都在.cvs
中。现在,您可以将每种情况分配给特定的变量,也可以将它们放在对象列表中。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。