我正在使用视觉工作室,并且这个程序从一个名为inmpg的txt文件中获取数据,然后打印出一个名为outmpg.txt的生成文件。代码没有错误,但是当控制台窗口出现时,它只能进入“从文件读取”而不会从那里继续。 “outmpg.txt”已创建但空白。任何帮助表示赞赏。
#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1
float amt2; // # of gallons for fillup 2
float amt3; // # of gallons for fillup 3
float amt4; // # of gallons for fillup 4
float startMiles; // Starting mileage
float endMiles; // Ending mileage
float mpg; // Computed miles per gallon
ifstream inMPG; // Holds gallon amts & mileages. Input
ofstream outMPG; // Holds miles per gall. Output
// Open the files
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
cout << "can't find inmpg.txt" << endl;
return 0;
}
outMPG.open("outmpg.txt");
if (outMPG.fail())
{
cout << "can't create/ open outmpg.txt" << endl;
return 0;
}
// Get data (priming read)
cout << "Reading from file" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
while (!inMPG.eof())
{
// Compute miles per gallon
mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
// Output results
cout << "wrote to file outmpg.txt" << endl;
outMPG << "For the gallon amounts" << endl;
outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
outMPG << "and a starting mileage of " << startMiles << endl;
outMPG << "and an ending mileage of " << endMiles << endl;
outMPG << "the mileage per gallon is " << mpg << endl;
cout << "\n Reading the next set of data" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
}
return 0;
}
答案 0 :(得分:0)
问题来自于您第一次从inMPG文件开始阅读时。如果您只有一个条目,则循环永远不会执行,并且会跳过它,因为它已经到达文件的末尾。因此,永远不要写入outMPG文件。
我所做的是删除while循环上方的两行,并将它们放在while循环内部的开头,并删除循环内的最后两行。
另一个问题是.eof()调用。至于为什么它在这种情况下实际上不起作用超出了我,但我使用了.peek()。所以语句可以改为while(inMPG.peek()!= EOF)。
这是为我工作的代码片段
#include <iostream>
#include <fstream> // For file I/O
using namespace std;
int main()
{
float amt1; // # of gallons for fillup 1
float amt2; // # of gallons for fillup 2
float amt3; // # of gallons for fillup 3
float amt4; // # of gallons for fillup 4
float startMiles; // Starting mileage
float endMiles; // Ending mileage
float mpg; // Computed miles per gallon
ifstream inMPG; // Holds gallon amts & mileages. Input
ofstream outMPG; // Holds miles per gall. Output
// Open the files
inMPG.open("inmpg.txt");
if (inMPG.fail())
{
cout << "can't find inmpg.txt" << endl;
return 0;
}
outMPG.open("outmpg.txt");
if (outMPG.fail())
{
cout << "can't create/ open outmpg.txt" << endl;
return 0;
}
while(inMPG.peek() != EOF)
{
cout << "Reading the next set of data" << endl;
inMPG >> amt1 >> amt2 >> amt3 >> amt4 >> startMiles >> endMiles;
// Compute miles per gallon
mpg = (endMiles - startMiles) / (amt1 + amt2 + amt3 + amt4);
// Output results
cout << "wrote to file outmpg.txt" << endl;
outMPG << "For the gallon amounts" << endl;
outMPG << amt1 << ' ' << amt2 << ' ' << amt3 << ' ' << amt4 << endl;
outMPG << "and a starting mileage of " << startMiles << endl;
outMPG << "and an ending mileage of " << endMiles << endl;
outMPG << "the mileage per gallon is " << mpg << endl;
}
return 0;
}
我的输入文件是这个;
24.0 25.0 23.0 20.0 30.0 50.0