将变量与txt文件中的每一行进行比较
Compare variable with each line from a txt file
我必须将txt文件的每一行与用户输入变量进行比较。如果用户输入的单词存在于txt文件中,它应该提示用户"单词存在",如果没有,则退出程序。
这就是文本文件的样子:
hello
hey
wow
your
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream file("words.txt");
string content;
string userinput;
while(file >> content) {
cout << content << endl; // gets all the lines from the txt file
while(userinput != "exit") {
// asks user for input
cin >> userinput;
// compares two inputs
if (userinput == content)
{
cout << "The word exists." << endl;
} else {
break;
}
if (userinput == "exit") {
break;
}
}
}
return 0;
}
这对我来说不起作用。我可以返回txt文件中的所有单词,但无法将用户输入的文本与txt文件的txt行进行比较。任何帮助都会很棒。谢谢
更新代码:
while(iFile >> content) {
while(userinput != "exit") {
// asks user for input
cin >> userinput;
// compares two inputs
if (content.find(userinput) != std::string::npos)
{
cout << "The word exists." << endl;
} else {
break;
}
if (userinput == "exit") {
break;
}
}
}
p.S:我对c++还很陌生。学生
#include <string>
#include <iostream>
#include <fstream>
#include <unordered_set>
using namespace std;
int main()
{
fstream file("wy.txt");
string line;
unordered_set<string> res;
while(file>>line)
{
res.insert(line);
}
do
{
cout<<"Please input the word: "<<endl;
string str;
cin>>str;
if (res.find(str) != res.end())
cout << "The word exists." << endl;
else
break;
} while (true);
}
此代码可以正常工作。
您正在文件中的每一行标记上运行一个循环,在该循环中,您询问用户的猜测,直到他放弃每个猜测?不错,但不是你说的那样。
你想做什么:
- 读取文件,将其解析为保存在
std::unordered_set
中的单词标记 - 然后询问用户应该匹配哪个单词
- 要求
std::unordered_map
泄露其秘密
试试看。
在编写时:
// compares two inputs
if (userinput == content)
您确实在检查用户是否在内容中输入了精确的文本。您想要的是检查用户输入是否包含在内容中:
// check if the userinput is found in the text
if(content.find(userinput) != std::string::npos)
您还需要阅读完整的文件。现在,每次向用户请求输入时,您都要读取输入文件。
#include <iostream>
#include <fstream>
#include <memory>
#include <sstream>
#include <sys/types.h>
#include <sys/stat.h>
inline static bool fileExists(const std::string& name) {
struct stat buffer;
return (stat (name.c_str(), &buffer) == 0);
}
/// Read the file content and return as a string
static std::string readFile(const std::string &filename)
{
std::ifstream t(filename);
std::stringstream buffer;
buffer << t.rdbuf();
return buffer.str();
}
int main() {
std::string filename("words.txt");
if(!fileExists(filename))
// print error
return -1;
string content = readFile(filename);
string userinput;
... // handle userinput from now
请注意,这是低效的。由于您的文本总是相同的,并且您正在重复搜索,因此可以对其进行预处理。有多种数据结构可以提供帮助。例如,您可以有一个散列映射,并用每一行作为关键字填充它。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 用c++从输入文件中读取另一行
- 读取文件的最后一行并输入到链接列表时出错
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- 从C++dll访问C#中的一行主要参数
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 在C++中,我如何接受不同于同一行的用户输入
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- C++将目录中的所有文件与::filesystem进行比较
- 比较两个没有一行共同点的大文件
- GCC:为什么一行代码中的错误(字符串和NULL的比较)会导致一长串错误消息
- 从文本文件中读取特定的一行,并将其与输入进行比较
- 将变量与txt文件中的每一行进行比较