在c中读取文件时fstream无限循环
作者:互联网
我在学校的CS101课堂上使用C语言.我有一个程序必须以这种格式读取文件:
Ramirez, Manny
1572838992 a 4 b 5 x 4 a 7 c 3 c 4 *
Kemp, Matt
3337474858 a 4 b 4 b 4 a 4 *
它应该读取文件,计算每个学生的GPA并输出学生的姓名,ID,单位和GPA.我已经尝试了一段时间不同的事情,而且我一直在无限循环中来回走动.我得到了一位朋友的帮助,他让我加入了一些东西.
我已经尝试在while(!eof)循环中改变循环的条件这么多次我无法统计它们.每当我查看在线做什么时,我得到的建议就是不要使用while!eof,因为它有导致无限循环的倾向.好吧,我现在知道了,但我的教授希望我们这样做.
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
using namespace std;
int main() {
char grade;
int units=0, studentsGrade=0, studentUnits=0;
int unitTotal=0;
float gradeTotal=0, gpa = 0;
string inputName, outputName, fullName;
long ID;
ifstream inputFile;
ofstream outputFile;
cout << "Please enter the input filename: ";
cin >> inputName;
cout << "Please enter the output filename: ";
cin >> outputName;
inputFile.open(inputName.c_str());
if (inputFile.fail())
cout << "Bad input file name." << endl;
else {
outputFile.open(outputName.c_str());
outputFile << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
<< "Units" << setw(15) << "GPA" << endl;
outputFile << "--------------------------------------------------------------------------------" << endl;
cout << left << setw(25) << "Name" << setw(15) << "ID" << setw(15)
<< "Units" << setw(15) << "GPA" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
getline(inputFile, fullName);
while (!inputFile.eof()) {
gpa = 0;
unitTotal = 0;
gradeTotal = 0;
inputFile >> ID;
outputFile << setw(25) << fullName;
outputFile << setw(15) << ID;
cout << setw(25) << fullName << setw(15) << ID;
string line;
getline(inputFile,line);
istringstream iss(line);
while (!iss.eof()) {
units = 0;
iss >> grade >> units;
if (grade == '*')
break;
if (units > 0 && units <=5 && (grade == 'a' || grade == 'A')) {
gradeTotal += 4 * units;
studentsGrade += 4 * units;
unitTotal += units;
studentUnits += units;}
else if (units > 0 && units <=5 && (grade == 'b' || grade == 'B')) {
gradeTotal += 3 * units;
studentsGrade += 3 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'c' || grade == 'C')) {
gradeTotal += 2 * units;
studentsGrade += 2 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'd' || grade == 'D')) {
gradeTotal += 1 * units;
studentsGrade += 1 * units;
unitTotal += units;
studentUnits += units; }
else if (units > 0 && units <=5 && (grade == 'f' || grade == 'F')) {
unitTotal += units;
studentUnits += units; }
else if (grade == '*') {
unitTotal += 0;}
else {
unitTotal += 0; }
}
gpa = (float)gradeTotal / unitTotal;
outputFile << fixed << showpoint;
outputFile << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
cout << fixed << showpoint;
cout << setw(15) << unitTotal << setw(15) << setprecision(2) << gpa << endl;
getline(inputFile,fullName);
}
outputFile << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits;
cout << "The GPA for all students is " << setprecision(2) << (float)studentsGrade / studentUnits << endl;
}
inputFile.close();
outputFile.close();
cout << endl;
return 0;
}
如果有人能向我解释为什么我会继续获得无限循环,我会非常感激.
解决方法:
虽然需要通过运行代码进行验证,但似乎你得到的是failbit或badbit,而不是eof.这可能发生,例如,在您执行其他读取而不是getline()之后 – 例如inputFile>> ID;.
由于您只检查eof,循环无休止地运行,I / O错误被忽略.我想尝试使用getline()的结果并使用std :: istream的operator void *而不是检查eof().
如果您的教授希望您仅使用eof(),请尝试解决错误检查问题.解决方案可能是使用特殊值并假设如果读取操作没有将值设置为其他值,那么它就会失败,并且会脱离循环.例如,在读取之前将ID设置为LONG_MAX,并确保它之后不是LONG_MAX.
毕竟 – 只需在一个小型工作数据集上运行调试器下的代码,然后逐步查看会发生什么.熟悉调试器并练习故障排除技巧.在您的职业生涯中,这肯定会为您节省大量时间.
另见 – http://www.cplusplus.com/reference/iostream/ifstream/
祝好运!
标签:c,infinite-loop 来源: https://codeday.me/bug/20190902/1790505.html