如何在文本文件中搜索员工记录(按姓名)并仅显示其详细信息?
Posted
技术标签:
【中文标题】如何在文本文件中搜索员工记录(按姓名)并仅显示其详细信息?【英文标题】:How can I search for a employee record (by name) in a text file and only display it's details? 【发布时间】:2019-06-29 08:46:07 【问题描述】:我正在制作一个程序,在其中输入许多员工的详细信息并将其存储在文本文件中。如何创建一个功能来搜索整个文本文件并显示该员工的唯一详细信息而不是其他任何人的详细信息? 详细信息始终以附加模式输入。我不能使用 eof(),因为它会显示整个文档。
这是一个学校项目,我们只研究过cin和cout,而不是std::,因此我使用的是using namespace std;
编辑:添加示例文本文件
First name:Test
Last name: asdfas
Employee no: 12
(etc.)
Local Contact: 12323
***********************************************
First name:Test2
Last name: asd
Employee no: 23432
(etc.)
Local Contact: 234324
***********************************************
void hr::empdetails()
//declaring all datamembers
char firstname [30], lastname [30], dept[30]; //etc.
ofstream outfile;
outfile.open ("example.txt",ios::app);
//inputting all details
//writing details into text file...
outfile<<"First name:";
outfile<<firstname;
//...................
outfile<<"\nLocal Contact: ";
outfile<<localcon;
outfile<<"\n\n*************************************************";//indicating end of employee's details
void hr::searchname()
//what should i write here to search for a name and display all of its details
【问题讨论】:
使用eof
,但也使用if
语句仅显示您感兴趣的员工而不是所有员工。
@john "使用eof
" 我不建议这样做:Why is iostream::eof inside a loop condition (i.e. while (!stream.eof())
) considered wrong?
提供员工样本文件....
@john 怎么做?
@sha111 已附上
【参考方案1】:
在大多数情况下,方法是读取记录中的所有字段,并且只使用您需要的字段。与执行代码跳过它们相比,读取额外的字段不会花费任何额外的时间。
此外,与并行数组相比,更喜欢结构数组 (std::vector
):
struct Employee_Record
std::string first_name;
std::string last_name;
int id;
//...
;
std::vector<Employee_Record> database;
Employee_Record array[32];
您可以通过为结构重载operator>>
来简化输入:
struct Employee_Record
//...
friend istream& operator>>(istream& input, Employee_Record& er);
;
istream& operator>>(istream& input, Employee_Record& er)
getline(input, er.first_name);
getline(input, er.last_name);
//...
return input;
你输入的代码看起来像这样:
std::vector<Employee_Record> database;
Employee_Record er;
while (data_file >> er)
database.push_back(er);
一种常见的技术是读入所有数据,然后对数据进行处理(例如搜索)。
【讨论】:
【参考方案2】:int main()
ifstream fin("look.txt");. // Here you have to provide file name
string line; // takes a line at a time.
int person = 1; // this increments person
while (getline(fin, line)) // here we are reading data line by line till eof
if (line == "***********************************************") // this is point where we increment the person variable by one ( to change person )
person++;
int ind = line.find_last_of(':'); // here we are finding ':' character to get fields name like First Name , Last Name ,etc..
string cc = line.substr(0, ind); // here we get value of the fields ex:- First Name :-Sha11 ( here we fetch Sha11 .. you use this way to compare empolyees various value ,in your desired way.. )
if (cc == "First name" || cc == "Last name" || cc == "Local Contact") ( It is looking only for some desired fields , but you might change this according to you. )
if (ind != string::npos)
int diff = line.size() - ind - 1;
string pa = line.substr(ind + 1, diff);
cout << person << " : " << cc << " : " << pa << endl; // here cc stores the field's name and pa stores the field's value. here i used substr() and find() to get desired results from the string (for more details about these function look at these urls "www.cplusplus.com/reference/string/string/find/" , "http://www.cplusplus.com/reference/string/string/substr/")..
return 0;
这个注释解释可能会对你有所帮助......!
这可能会解决您的问题....
【讨论】:
请解释您的代码如何解决 OP 的问题或问题,而不是发布解决方案。我们更愿意让 OP 学习编写自己的代码,而不是给他们代码。如果没有给出任何解释,我们如何知道您的代码有效?以上是关于如何在文本文件中搜索员工记录(按姓名)并仅显示其详细信息?的主要内容,如果未能解决你的问题,请参考以下文章
Oracle APEX Application Builder:搜索特定员工并在硬编码文本字段中返回他们的姓名、薪水和部门