使用逗号解析文件对象?

Parse a File Object using Commas?

本文关键字:文件 对象      更新时间:2023-10-16

我正在尝试使用逗号而不是空格正确解析文件对象 RecordFile,然后将数据分配给正确的变量。我目前正在使用空格进行解析,如下面的记录示例所示。有什么建议吗?

ifstream RecordFile("records.txt"); 
while (!RecordFile.eof())    
{
string f, l, aba, id, e;                          
RecordFile >> f >> l >> aba >> id >> e;           
Recordsarray[i].setRecords(f, l, aba, id, e);     
i++;
recordcounter++; 
}
RecordFile.close();            
return recordcounter;          

示例记录:

Albert Green 000000000 8181234567 a.green@piercecollege.edu

你可以使用boost::splite函数:

int func()
{
ifstream RecordFile("records.txt"); 
std::string line;
while (!std::getline(RecordFile, line);)    
{
std::vector<string> vec;
boost::split(vec, line, boost::is_any_of(","));
Recordsarray[i].setRecords(vec);     
i++;
recordcounter++; 
}
RecordFile.close();            
return recordcounter;
}