如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?
How can I search for a employee record (by name) in a text file and only display it's details?
我正在制作一个程序,其中我输入了许多员工的详细信息并将其存储在文本文件中。如何制作函数来搜索整个文本文件并显示该员工而不是其他人的唯一详细信息?详细信息始终以附录模式输入。我无法使用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;
}
此评论的解释可能会帮助您...
这可能会解决您的问题..
相关文章:
- std::extent 实现详细信息说明
- VisualC++ 2010 有没有办法找出有关未处理异常错误的更多详细信息
- 如何使用软化工具包从 OPC UA 服务器异步读取操作回调中的数据值响应中获取 NodeId 详细信息
- 错误:调用'strcmp'没有匹配函数(尝试设置显示用户信息功能)
- protobuf,如何在protobuf消息中遍历所有集合字段,我不知道详细信息?(C++)
- AVX2收集指令使用详细信息
- 只知道运行时的数据类型.如何将数据详细信息隐藏到使用它们的其他类
- CppUnit:如何立即打印故障详细信息
- 在Bazel中显示编译器信息
- 我可以为 C++ 类提供不完整的标头以隐藏实现详细信息吗?
- CPP 异常获取抛出调用方的详细信息
- 内存映射 IO 概念详细信息
- 如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?
- 使用TDD时隐藏文件访问实现详细信息
- 如果构造函数从调用到 std::make_shared 崩溃,GDB 是否可以显示崩溃的详细信息
- 资源管理器在 TILE 视图模式下不显示 shell 命名空间扩展的项目详细信息
- 显示 txt 文件详细信息
- 如何在Linux终端的目录下显示详细的文件信息?-C++
- 程序显示学生的详细信息,如名字父亲名字滚动没有CGPA输出
- 从显示输出中最后一行详细信息的文本文件中读取两次