我有一个txt文件,其中包含名称,身份证号码,mobilenumber和位置以逗号分隔的行。 例如 罗比,7890,7788992356,123威斯敏斯特 汤姆,8820,77882345,124京士顿街 我的任务是找回如何操作使用C++ STL的txt文件HOmework
通过名称查找所有的员工的信息。 按ID查找员工的所有信息。 添加员工的信息。 更新员工的信息。
到目前为止我已经读取了该文件并将其存储在向量中。代码如下所示。 对于任务 1)按名称查找员工的所有信息。我将遍历矢量并打印包含名称的信息。我将能够做到这一点 2)在文本文件simialry我会寻找id并打印有关信息。 但我无能点3 & 4.
我通过分割CSV行成独立的字段,然后张贴下面
void filter_text(vector<string> *words, string name)
{
vector<string>::iterator startIt = words->begin();
vector<string>::iterator endIt = words->end();
if(!name.size())
std::cout << " no word to found for empty string ";
while(startIt != endIt)
{
string::size_type pos = 0;
while((pos = (*startIt).find_first_of(name, pos)) != string::npos)
std:cout <<" the name is " << *startIt<< end;
startIt++;
}
}
int main()
{
// to read a text file
std::string file_name;
std::cout << " please enter the file name to parse" ;
std::cin >> file_name;
//open text file for input
ifstream infile(file_name.c_str(), ios::in) ;
if(! infile)
{
std::cerr <<" failed to open file\n";
exit(-1);
}
vector<string> *lines_of_text = new vector<string>;
string textline;
while(getline(infile, textline, '\n'))
{
std::cout <<" line text:" << textline <<std::endl;
lines_of_text->push_back(textline);
}
filter_text(lines_of_text, "tony");
return 0;
}
这里有很多不完整的句子和想法。 –
如何标记每行(以逗号分割)并将结果存储在'std :: map'中? –
如果这是作业,请添加'作业'标签。 –