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

时间:2019-06-29 08:46:07

标签: c++ c++11 visual-c++ file-handling

我正在编写一个程序,在其中输入许多员工的详细信息,并将其存储在文本文件中。我该如何搜索整个文本文件并仅显示该员工的详细信息,而不显示任何其他人的详细信息? 细节总是以附加模式输入。我不能使用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<<"\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
}

2 个答案:

答案 0 :(得分:0)

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

此评论注释可能会帮助您...!

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

答案 1 :(得分:0)

在大多数情况下,方法是读取记录中的所有字段,并且仅使用所需的字段。与执行代码以跳过它们相比,读取额外的字段不会花费任何额外的时间。

此外,与并行数组相比,更喜欢结构的数组(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);
}

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