gpt4 book ai didi

c++ - 在文本文件中查找特定单词?

转载 作者:行者123 更新时间:2023-11-28 06:09:49 25 4
gpt4 key购买 nike

所以我对 C++ 和编码比较陌生,我最近尝试制作这个调查程序(请忽略可怕的代码)。我陷入困境的事实是,最后,当用户询问现有信息时,我无法在文本文件中找到带有名称的特定信息。我该怎么办?此外,ExisitingUser 之前的 goto 标签显示类似 -fpermissive 的错误。不知道那是什么。

如果之前有人回答过类似的问题,我们深表歉意。找不到它。

代码:

#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/E\n";
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/YYYY\n";
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 语句。当然,凡事都有异常(exception),但它们很少见,这不是其中之一。 https://xkcd.com/292/

任何人都很难理解您的代码,但我会专注于您的直接问题,即在文本文件中查找单词。如果你的文本文件是非结构化的并且不是特别大,那么最简单的方法可能是将它放入一个字符串中,然后调用字符串的 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 luck\n";
else
std::cout << "found it!\n";

第一个示例中的中断是一种非常常见的完成方式,对于较小的单个循环,很清楚会发生什么。事实上,我敢打赌有些人会争辩说第一个更可取,因为它更简洁明了。

一般来说,您只是要小心让代码到处跳转。 Gotos 就是这样做的,如果你有大的嵌套循环,breaks 可以做到这一点。有人试图跟随你的代码遇到它,然后必须弄清楚封闭循环是什么,然后记住外循环中发生了什么。

关于c++ - 在文本文件中查找特定单词?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31503061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com