如何解决如何比较文件c ++中的列
我有一个包含 3 列多行的文件。我想比较 2 列的增加或减少值。但是当我比较时,即使应该减少该值也总是增加。知道如何解决这个问题吗?这是我用来比较文件中值的编码。但我没有得到想要的输出。有人可以帮我吗?
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream fromFile;
fromFile.open("data.txt");
{
cout << "There was an error" << endl;
exit(1);
}
else
cout << "No error,file opened successfully" << endl;
fromFile.clear();
fromFile.seekg(0);
for(int i=1; i<=20; i++)
{
int a{},b {},c {};
fromFile >> a >> b >> c ;
cout<<" Year : " <<a<<" population : "<<b<<" car : "<<c<< endl;
if ( b < b && c < c )
cout << " population decrease and car production decrease " << endl;
if ( b > b && c > c );
cout << " population increase and car production increase " << endl;
if ( b > b && c < c )
cout << " population increase but car production decrease " << endl;
if ( b < b && c > c );
cout << " population decrease but car production increase " << endl;
}
//CLOSES FILES
fromFile.close();
return 0;
}
解决方法
您的代码有几个问题。我将首先提及这些,然后展示更正的解决方案(许多可能的变体之一)。
一些问题:
- 您不应使用
using namespace std
。 stackoverflow 上有很多解释。改用全限定名 -
std::ifstream
具有自动打开流的构造函数。不需要函数open
- 流有一个重载的 bool 运算符。您可以简单地使用
if (fromFile)
测试状态
- 您忘记了
if
语句,但有一个else
语句。因此,您的程序将无法编译。并且在逻辑上是错误的 - 由于缺少
if
,程序将始终立即退出 -
clear
和seekg
不是必需的。打开流后,文件指针位于开头。 - 您使用了一个带有固定硬编码幻数 20 的循环。如果文件有更多或更少的行怎么办?您需要使用条件结束文件来结束循环
- 您总是将变量与其自身进行比较。甚至编译器也会警告你。这是行不通的。您需要读取一个变量,将其存储在不同变量中的某个位置,再次从文件中读取并将当前变量与先前值进行比较。这是您的主要问题
- 无需调用流的
close
函数。当流变量离开其作用域时,析构函数会为您隐式调用它 - 您没有足够的比较
if
语句。很多情况下,特别是如果值与以前相同,将不会处理
那么,我们需要做什么?我现在只用一个变量来解释。让我们以年为例。我们定义了一个变量 year
和第二个变量 previousValueYear
。在循环中,我们将文件中的值读入 year
。然后,我们将 year
变量与 previousValueYear
进行比较,进行评估并显示输出。然后,在从文件中读取下一个值之前,我们将当前值从 year
复制到 previousValueYear
。然后我们可以在下一次循环运行中进行正确的比较。
第一个比较有点棘手,因为我们没有 previousValueYear
。我们将添加一个带有布尔标志的额外 if
语句,以检查这种情况。
请在此处查看更正后的版本:
#include <iostream>
#include <fstream>
int main() {
// Open the source file
std::ifstream fromFile("data.txt");
// Check,if we could open the file
if (fromFile) { // bool operator of stream will be called
// OK,file could be opened. Defineour variables
int year{},population{},car{};
int previousValueYear{},previousValuePopulation{},previousValueCar{};
bool thisIsTheNotFirstLoopRun{};
// Read values in a loop and check,if the input worked
while (fromFile >> year >> population >> car) {
// Show read data
std::cout << "Year: " << year << " \tPopulation: " << population << " \tCar: " << car << '\n';
// In the first loop run,we will not do a comparision (There is only one value and nothing to compare
if (thisIsTheNotFirstLoopRun) {
// Do the comparisons
if ((population < previousValuePopulation) and (car < previousValueCar))
std::cout << "Population decrease and car production decrease\n";
if ((population > previousValuePopulation) and (car > previousValueCar))
std::cout << "Population increase and car production increase\n";
if ((population > previousValuePopulation) and (car < previousValueCar))
std::cout << "Population increase and car production decrease\n";
if ((population < previousValuePopulation) and (car > previousValueCar))
std::cout << "Population decrease and car production increase\n";
}
// Remember current values for next loop run
previousValueYear = year;
previousValuePopulation = population;
previousValueCar = car;
// Now,we will not have any longer the first loop run
thisIsTheNotFirstLoopRun = true;
}
}
else {
// Error,file could not be opened. Show message
std::cerr << "\nError: SourceFile could not be opened\n";
}
return 0;
}
高级 C++ porgrammers 会改变很多。不过暂时没问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。