如何在文件中搜索关键字,然后打印其中包含的字符串

How to search a file for a keyword, and then print the string it is contained in?

本文关键字:打印 包含 字符串 然后 关键字 文件 搜索      更新时间:2023-10-16

我目前正在制作一个电话簿。一旦用户输入了他们的数据,它就会将所有数据保存为一个字符串(姓名、地址、电话号码、电子邮件)。我对C++很陌生。任何帮助都将不胜感激。谢谢

有两个主要问题:

  1. 第一个联系人保存到文件后,正在进行的联系人不会显示在文件中

  2. 我想让用户可以搜索他们作为联系人输入的姓名或一个关键词,并获得包含他们所有信息的整个字符串。我该怎么做?


#include <iostream>
#include <string> 
#include <ostream> 
#include <fstream>   
using namespace std;
struct person{
string Name;
string Address;
string PhoneNumber;
string Email;
};
int main() {
ResetUserSelection: 
int userselection = 0;
cout << "Press 1 to Add Contact" << endl;
cout << "Press 2 to Search for Contact"<<endl;
cout << "What do you want to do? ";
cin >> userselection;
if(userselection == 1) {   
person newPerson;
cout << endl << "What is your Name? ";   
cin >> newPerson.Name;
cout << "What is your Address? " ;
cin >> newPerson.Address;
cout << "What is your Phone Number? " ;
cin >> newPerson.PhoneNumber;     
cout << "What is your Email? ";
cin >> newPerson.Email;
cout << endl;
cout << newPerson.Name<<"'s contact info"<<endl;
cout << "Name : " << newPerson.Name <<endl;
cout << "Address : " << newPerson.Address <<endl;
cout << "Phone Number : " << newPerson.PhoneNumber <<endl;
cout << "Email : " << newPerson.Email <<endl;
cout << endl;
string fullContact = newPerson.Name + " " + newPerson.Address + " " +       newPerson.PhoneNumber + " " + newPerson.Email; 
ofstream myfile;
myfile.open ("contactlist.txt");
myfile << fullContact;
myfile.close();
goto ResetUserSelection;
} 
else { 
string search;
ifstream Myfile;
Myfile.open ("contactlist.txt");
cout << "Who do you want to search for?" << endl;
cin >> search;
//i do not know what to do here
goto ResetUserSelection;
}
}

您的代码存在多个问题:

  1. 当你打开文件时,你不向它添加新数据,而是替换它。所以每次文件中只有一条记录。你需要在附加模式下打开文件,比如这个

    myfile.open ("contactlist.txt", ios::app);
    
  2. 您不分离记录,可能希望在将联系人写入文件时添加n,并添加类似,的内容来分离其字段。

    string fullContact = newPerson.Name + "," + newPerson.Address + "," + newPerson.PhoneNumber + "," + newPerson.Email + "n";
    
  3. 您不能在姓名或地址中添加带有空格的联系人。原因是您使用了operator >>。您可以将其替换为getline功能(但您需要在第一次读取操作后添加cin.ignore(256, 'n');,请参阅此)

    cin >> userselection;
    cin.ignore(256, 'n');
    if(userselection == 1) {
    cout << "What is your Name? ";
    getline(cin, newPerson.Name);
    cout << "What is your Address? " ;
    getline(cin, newPerson.Address);
    cout << "What is your Phone Number? " ;
    getline(cin, newPerson.PhoneNumber);
    cout << "What is your Email? ";
    getline(cin, newPerson.Email);
    ....
    }
    
  4. 回答您关于如何实现搜索的主要问题。一种方法是打开文件,逐行读取,在行中搜索出现的搜索字符串,如果找到了,打印

    ifstream myfile;
    myfile.open ("contactlist.txt");
    string contact;
    while(getline(myfile, contact)) {
    if(contact.find(search) != string::npos) {
    cout << contact << endl;
    }
    }
    myfile.close();
    

    也可以用getline(cin, search);替换cin >> search;

这不是一个确切的答案,但对您的程序有一些提示。

当你想写一个管理电话簿的应用程序时,你必须:

  • 确定要存储的信息(此处为姓名、地址、电话号码、电子邮件),并定义每个信息的格式(数字与字符串、大小、字符串允许的字符、大小写转换…)
  • 定义一种存储格式(文本、二进制),并确保在文本格式的情况下,它不会对您的信息格式产生歧义

这里,您使用空格作为分隔符,但似乎并不禁止在名称或地址中使用空格。第二个要求没有得到满足,你肯定会遇到麻烦。你最好使用一个不太可能出现在你的信息中的字符作为分隔符,比如|;。或者,您可以研究CSV格式如何处理转义特殊字符。

因此,除了Nikolai所说的以追加模式打开文件的问题之外,我强烈建议您使用换行符作为记录分隔符,使用|作为字段分隔符,并确保任何字段都不能包含这些分隔符。

由于您希望能够搜索您的电话簿,我还建议您以大写(或全部小写但不混合)存储姓名:比较会更容易,如果您以后使用数据库,则需要正确索引。