我有一个文本文件。 存储在其中的所有值都是整数。 当我比较我搜索的数字和存储在文本文件中的数字时,结果不正确!
我的代码:
ifstream infile ("h1.txt");
if (!infile)
{
cout << "Can't open file" << endl;
exit (EXIT_FAILURE);
}
int n;
infile >> n;
while(!infile.eof()){
if (n!=search)
{
return false;
}
else{
return true;
}
infile>>n;
}
为什么即使 n
等于搜索,编译器仍认为结果为 false?
最佳答案
无论正确与否,您只是从文件中读取第一个数字。当代码到达 return
时,函数的执行结束。
关于c++ - 在文本文件中搜索整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43702137/