如何在文件中搜索关键字,然后打印其中包含的字符串
How to search a file for a keyword, and then print the string it is contained in?
我目前正在制作一个电话簿。一旦用户输入了他们的数据,它就会将所有数据保存为一个字符串(姓名、地址、电话号码、电子邮件)。我对C++很陌生。任何帮助都将不胜感激。谢谢
有两个主要问题:
-
第一个联系人保存到文件后,正在进行的联系人不会显示在文件中
-
我想让用户可以搜索他们作为联系人输入的姓名或一个关键词,并获得包含他们所有信息的整个字符串。我该怎么做?
#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;
}
}
您的代码存在多个问题:
-
当你打开文件时,你不向它添加新数据,而是替换它。所以每次文件中只有一条记录。你需要在附加模式下打开文件,比如这个
myfile.open ("contactlist.txt", ios::app);
-
您不分离记录,可能希望在将联系人写入文件时添加
n
,并添加类似,
的内容来分离其字段。string fullContact = newPerson.Name + "," + newPerson.Address + "," + newPerson.PhoneNumber + "," + newPerson.Email + "n";
-
您不能在姓名或地址中添加带有空格的联系人。原因是您使用了
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); .... }
-
回答您关于如何实现搜索的主要问题。一种方法是打开文件,逐行读取,在行中搜索出现的搜索字符串,如果找到了,打印
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所说的以追加模式打开文件的问题之外,我强烈建议您使用换行符作为记录分隔符,使用|
作为字段分隔符,并确保任何字段都不能包含这些分隔符。
由于您希望能够搜索您的电话簿,我还建议您以大写(或全部小写但不混合)存储姓名:比较会更容易,如果您以后使用数据库,则需要正确索引。
- C++ 打印包含数据结构的数组
- 有没有办法搜索向量的元素,<String>然后检查它是否包含特定的字符,如果它确实打印了它
- 我该如何循环遍历我的数组(缓冲区——包含一个文本文件),并将其打印成30字节的块
- 打印/修改类对象的特定成员变量,其类定义列表 (STL) 包含的元素类型
- 如果该行包含特定单词 (C++),请打印该行
- 如何在文件中搜索字符串并打印包含该字符串的行
- 有没有办法打印包含当前文件的文件的文件名
- cout 打印的字符 [] 包含的字符数超过设置长度
- 我正在尝试读取一个包含字符串和整数的二进制文件..并打印出与字符串对应的整数
- 如何打印包含整数和整数集的std::映射
- 打印每行包含 n 个数字的数组
- 尝试使用 cout 打印矢量中包含的对象列表并排序是错误的
- 如何打印包含集合的地图的内容
- 打印与BST的直径相对应的节点所包含的路径
- 如何通过包含要打印的字符串的类函数打印异常
- 如何在文件中搜索关键字,然后打印其中包含的字符串
- 打印包含多映射的deque
- 在c++中的graphics.h上打印包含字符串的集合
- 打印包含 2D 数组的结构
- 打印包含映射的结构