如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?

How can I search for a employee record (by name) in a text file and only display it's details?

本文关键字:显示 详细信息 文件 文本 搜索 记录      更新时间:2023-10-16

我正在制作一个程序,其中我输入了许多员工的详细信息并将其存储在文本文件中。如何制作函数来搜索整个文本文件并显示该员工而不是其他人的唯一详细信息?详细信息始终以附录模式输入。我无法使用EOF((,因为它将显示整个文档。

这是一个学校项目,我们只研究了CIN和COUT而不是STD ::,因此我正在使用命名空间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<<"nn*************************************************";//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
}

在大多数情况下,该方法是在A Record 中读取所有 fields ,仅使用所需的字段。阅读额外的字段不会花费任何额外的时间与执行代码来跳过它们。

另外,更喜欢结构的数组(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);
}

一种常见的技术是在所有数据中读取,然后处理数据(例如搜索(。

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;
}

此评论的解释可能会帮助您...

这可能会解决您的问题..