文件读取功能只读取第一行然后退出
File Reading Function only Reads First line then quits
问题描述
基本上,在我的 "Roster.h" header 文件中,我有一个来自学生 class 的 "Student" objects 数组(其中包括函数changeScore、SetID、setTotal、setLetterGrade)。在下面附上的函数中,它只是读取第一行数据,然后在 while 条件下退出。我已经盯着这个问题看了好几个小时了,可以使用第二(或第三)双眼睛。任何批评也表示赞赏,因为我知道我不是最有效的 programmer.It 应该注意 "m_studentnum" 是在构造函数中初始化为 0 的私有数据。提前致谢!
代码
void Roster::readStudentRecord(string file)
{
ifstream in;
string studentID;
string line;
int ola, cla, quiz, homework, exam, bonus, total, final = 0;
in.open(file.c_str());
getline(in, line);
while (in >> studentID) {
in >> cla >> ola >> quiz >> homework >> exam >> bonus >> total >> final;
m_students[m_studentNum].Student::setID(studentID);
m_students[m_studentNum].Student::changeScore(Student::CLA, cla);
m_students[m_studentNum].Student::changeScore(Student::OLA, ola);
m_students[m_studentNum].Student::changeScore(Student::QUIZ, quiz);
m_students[m_studentNum].Student::changeScore(Student::HOMEWORK, homework);
m_students[m_studentNum].Student::changeScore(Student::EXAM, exam);
m_students[m_studentNum].Student::changeScore(Student::BONUS, bonus);
total = cla + ola + quiz + homework + exam + bonus;
m_students[m_studentNum].Student::setTotal(total);
if (total >= 90) {
m_students[m_studentNum].Student::setLetterGrade('A');
}
else if (total >= 80 && total < 90) {
m_students[m_studentNum].Student::setLetterGrade('B');
}
else if (total >= 70 && total < 80) {
m_students[m_studentNum].Student::setLetterGrade('C');
}
else if (total >= 60 && total < 70) {
m_students[m_studentNum].Student::setLetterGrade('D');
}
else {
m_students[m_studentNum].Student::setLetterGrade('F');
}
m_studentNum++;
}
}
数据文件
-请注意,我正在执行 getline 以在数据列的标题中阅读
ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade
c088801 10 15 4 15 56 5
c088802 9 12 2 11 46 2
c088803 8 10 3 12 50 1
c088804 5 5 3 10 53 3
c088805 3 11 1 10 45 0
c088806 8 14 2 11 40 -1
c088807 4 12 2 12 48 -2
c088808 10 10 3 11 36 0
c088809 8 8 3 11 39 0
c088810 6 9 4 9 47 3
c088811 8 7 3 13 41 3
c088812 4 11 3 11 37 1
"Total"和"FinalGrade"栏为空,您无条件尝试阅读
当您尝试这样做时,输入将包含下一行的 "ID",因为它不是整数,导致为流设置 failbit
标志导致循环条件为假并结束循环。
一个可能的解决方案是将 while 行读入一个字符串,将该字符串放入一个 std::istringstream
对象,然后像现在一样读取 non-empty 列。然后尝试从输入字符串流中读取可能为空的列。
另一个解决方案,如果这些列应该是空的,就是不读它们。
问题描述
基本上,在我的 "Roster.h" header 文件中,我有一个来自学生 class 的 "Student" objects 数组(其中包括函数changeScore、SetID、setTotal、setLetterGrade)。在下面附上的函数中,它只是读取第一行数据,然后在 while 条件下退出。我已经盯着这个问题看了好几个小时了,可以使用第二(或第三)双眼睛。任何批评也表示赞赏,因为我知道我不是最有效的 programmer.It 应该注意 "m_studentnum" 是在构造函数中初始化为 0 的私有数据。提前致谢!
代码
void Roster::readStudentRecord(string file)
{
ifstream in;
string studentID;
string line;
int ola, cla, quiz, homework, exam, bonus, total, final = 0;
in.open(file.c_str());
getline(in, line);
while (in >> studentID) {
in >> cla >> ola >> quiz >> homework >> exam >> bonus >> total >> final;
m_students[m_studentNum].Student::setID(studentID);
m_students[m_studentNum].Student::changeScore(Student::CLA, cla);
m_students[m_studentNum].Student::changeScore(Student::OLA, ola);
m_students[m_studentNum].Student::changeScore(Student::QUIZ, quiz);
m_students[m_studentNum].Student::changeScore(Student::HOMEWORK, homework);
m_students[m_studentNum].Student::changeScore(Student::EXAM, exam);
m_students[m_studentNum].Student::changeScore(Student::BONUS, bonus);
total = cla + ola + quiz + homework + exam + bonus;
m_students[m_studentNum].Student::setTotal(total);
if (total >= 90) {
m_students[m_studentNum].Student::setLetterGrade('A');
}
else if (total >= 80 && total < 90) {
m_students[m_studentNum].Student::setLetterGrade('B');
}
else if (total >= 70 && total < 80) {
m_students[m_studentNum].Student::setLetterGrade('C');
}
else if (total >= 60 && total < 70) {
m_students[m_studentNum].Student::setLetterGrade('D');
}
else {
m_students[m_studentNum].Student::setLetterGrade('F');
}
m_studentNum++;
}
}
数据文件 -请注意,我正在执行 getline 以在数据列的标题中阅读
ID CLA OLA Quiz Homework Exam Bonus Total FinalGrade
c088801 10 15 4 15 56 5
c088802 9 12 2 11 46 2
c088803 8 10 3 12 50 1
c088804 5 5 3 10 53 3
c088805 3 11 1 10 45 0
c088806 8 14 2 11 40 -1
c088807 4 12 2 12 48 -2
c088808 10 10 3 11 36 0
c088809 8 8 3 11 39 0
c088810 6 9 4 9 47 3
c088811 8 7 3 13 41 3
c088812 4 11 3 11 37 1
"Total"和"FinalGrade"栏为空,您无条件尝试阅读
当您尝试这样做时,输入将包含下一行的 "ID",因为它不是整数,导致为流设置 failbit
标志导致循环条件为假并结束循环。
一个可能的解决方案是将 while 行读入一个字符串,将该字符串放入一个 std::istringstream
对象,然后像现在一样读取 non-empty 列。然后尝试从输入字符串流中读取可能为空的列。
另一个解决方案,如果这些列应该是空的,就是不读它们。