在文本文件中查找特定的单词
Finding a particular word in a text file?
所以我对c++和编码比较陌生,我最近尝试制作这个调查程序(请忽略可怕的代码)。我被困的地方是,在最后,当用户要求现有的信息,我没有办法找到特定的信息与名称在文本文件中。我该怎么办呢?此外,ExisitingUser之前的goto标签显示了类似于-fpermissive error的内容。我不知道那是什么
抱歉,如果这样的事情已经回答了之前。
代码:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
char choiceInfo;
cout << "Do you want to enter a new user or check existing info? N/En";
cin >> choiceInfo;
if(choiceInfo == 'N' || choiceInfo == 'n') {
} else {
goto existUser;
}
x:
string firstName,surName,fullName,DoB,quesOne,quesTwo,quesThree,quesFour,quesFive;
int age;
cout << "Enter your first name.n";
cin >> firstName;
cout <<"Enter your surname.n";
cin >> surName;
fullName = firstName + surName;
cout << "How old are you?n";
cin >> age;
cout << "What is your DoB?n Format:DD/MM/YYYYn";
cin >> DoB;
cout <<"What is your favorite sport?n";
cin >> quesOne;
cout <<"What is your favorite colour?n";
cin >> quesTwo;
cout <<"Who is your favorite celebrity?n Please enter all in one word.n";
cin >> quesThree;
cout <<"What is your favorite hobby?n";
cin >> quesFour;
cout <<"Which is your favorite quote?n";
cin >> quesFive;
cout << "Thank you for registering.";
ofstream writer("Users.txt");
writer << endl << endl << endl
<< "Full Name: " << fullName << endl
<< "Age: " << age << endl
<< "DOB: " << DoB << endl
<< "Answer to Question 1: "
<< quesOne<< endl
<< "Answer to Question 2: " << quesTwo << endl
<< "Answer to Question 3: " << quesThree << endl
<< "Answer to Question 4: " << quesFour << endl
<< "Answer to Question 5: " << quesFive << endl;
writer.close();
goto z;
existUser:
{
string userName;
char letter;
cout << "Enter full username.n";
cin >> userName;
ifstream reader("Users.txt");
if (! reader) {
cout << "Error opening file.";
} else {
char(letter);
for (int i = 0;! reader.eof(); i++) {
reader.get(letter);
cout << letter;
reader.close();
}
}
}
z:
return 0;
}
"忽略这些可怕的代码"这个要求太过分了。在任何编程语言中都不要使用goto语句。当然,凡事都有例外,但这种情况很少见,而且这次也不例外。https://xkcd.com/292/
任何人都很难理解你的代码,但我将专注于你的直接问题,即在文本文件中找到一个单词。如果您的文本文件是非结构化的,并且不是特别大,那么最简单的方法可能是将其放入字符串中,并调用string的find方法。Getline在字符串头文件中。
你已经熟悉ifstream了。但是,您一个字符一个字符地阅读文件,这是非常慢的,如果您想搜索文件,上天会帮助您。您可以逐行读取,并将每行存储在字符串中。如果您使用文本文件,您可能会经常这样做。Getline从流中读取,直到遇到换行符,它丢弃换行符,将该行加载到字符串中,并将流移动到下一行的第一个字符。Getline的返回值有点奇怪,但足以说明您可以直接在while循环中使用它,当它到达文件末尾时它将停止。如果找不到,则返回std::string::npos,这是一个特殊值,表示各种字符串函数中某种类型的失败。
std::ifstream file("yourfile.txt");
std::string line; //this is the string that we'll be storing the line in
while (getline(file, line)) //read a line of text from file into our line variable
{
if (line.find("text to find") != std::string::npos) //if we find our text in line
{
std::cout << "found it!n";
break; //no need to read the other lines
}
}
另一种方法,我可以这样做:
std::string line = "";
while (line.find("your text") == std::string::npos) //while I fail to find the line I'm looking for
{
getline(file,line); // keep reading in lines
}
if (file.eof()) //if I read through the whole file without finding anything
std::cout << "No luckn";
else
std::cout << "found it!n";
第一个例子中的break是一种很常见的方法,对于较小的单循环,发生的情况很清楚。事实上,我敢打赌有些人会认为第一种方式更可取,因为它更简洁,更清晰。
一般来说,您只需要小心让代码到处乱跳即可。Gotos就是这样做的,如果你有大的嵌套循环,break也可以这样做。试图跟踪你的代码的人会遇到它,然后必须弄清楚内循环是什么,然后记住外循环中发生了什么。
下面是一小段代码,它将在文件中搜索关键字:
std::string text_read;
bool text_found = false;
while (getline(reader, text_read))
{
if (text_read == fullname)
{
text_found = true;
break;
}
}
if (text_found)
{
cout << "Text found.n";
}
- 在C++中查找(奇怪的)字符串中的单词
- 从文件中查找单词并替换到其他文件中
- 在文本中查找半个单词"ABBA"的问题
- C++:如何用split查找单词?Regex
- 查找包含指定单词的行(从文件中)
- 创建程序以从给定的.txt文件中查找文本,替换并计算单词
- 如何创建一个函数,该函数在文本中查找单词的匹配项,包括跳过
- 在带有C++的txt文件中查找最长的单词
- 从文件的一行中查找数字的平均值,避免使用第一个单词
- 在文件中查找 10 个最长的单词
- 如何使我的单词查找更清晰、更高效
- 视觉C++查找以 "ing" 结尾且少于或等于 10 个字符的单词
- 如何在没有分隔符的大文本文件中查找所有字典单词
- 我试图编写一个代码来查找一个单词是否是回文,但它不起作用。怎么了?
- 我如何在一串随机生成的字母I C 中查找一个单词
- 在 C++ 的句子中查找某个单词
- Qt使用QWebEngine查找单词功能
- 在向量中通过一个单词查找行
- 单词搜索难题:如何搜索一系列字母来查找单词
- C 查找并替换整个单词