流不规则模式

ifstream irregular pattern

本文关键字:模式 不规则      更新时间:2023-10-16

我很难准确地指出我的文件输入出错的地方。下面是代码:

  char tempFirst[20], tempLast[20], tempCourse[7];
  char c;          // For peeking

  // Find out how many students are in the file
  inFile >> numStudents;
  for (int i = 0; i < numStudents; i++)
    {
      // Get last name from file
      inFile.getline(tempLast, 20, ',');
      // Gets rid of any spaces inbetween last and first
      while (isspace(inFile.peek()))
        c = inFile.get();
      // Get first name from file
      inFile.getline(tempFirst, 20, 'n');
      // New line, get course
      inFile >> tempCourse;
      // PRINT
      cout << tempFirst << "n" << tempLast << "n"
             << tempCourse << "n";
      list[i]->SetGrades(inFile);
    }

SetGrade将导致以下三个继承函数之一:

void EnglishStudent::SetGrades(ifstream& inFile)
{
  inFile >> attendance >> project >> midterm >> final;
  cout << attendance << " " << project << " " << midterm << " " << final << "nn";
}
void HistoryStudent::SetGrades(ifstream& inFile)
{
  inFile >> paper >> midterm >> final;
  cout << paper << " " << midterm << " " << final << "nn";
}
void MathStudent::SetGrades(ifstream& inFile)
{
  inFile >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5
         >> test1 >> test2 >> final;
  cout << quiz1 << " "<< quiz2 << " " << quiz3 << " " << quiz4 << " " << quiz5
       << " " << test1 << " " << test2 << " " << final << "nn";
}

这是我加载信息的文件:

6
Bunny, Bugs
Math 90 86 80 95 100 99 96 93
Schmuckatelli, Joe
History 88 75 90
Dipwart, Marvin
English 95 76 72 88
Crack Corn, Jimmy
Math 44 58 23 76 50 59 77 68
Kirk, James T.
English 40 100 68 88
Lewinsky, Monica
History 60 72 78

然后输出如下:

Bugs
Bunny
Math
90 86 80 95 100 99 96 93
Joe
History
88 75 90
Marvin
English
95 76 72 88
Jimmy
Crack Corn
Math
44 58 23 76 50 59 77 68
James T.
English
40 100 68 88
Monica
History
60 72 78

我遗漏了大多数姓氏,对于第一个学生,firstname有一个尾线。我该如何解决这个问题?

不是名字的末尾有一个换行符,而是姓氏的开头有一个换行符。当从输入中读取int s时,遇到的任何标记int结束的空白都将留在输入流中。

要解决这个问题,在SetGrades方法中或在循环结束时,在读取姓氏之前删除空白。后两者也需要在阅读numStudents后删除空白。删除空格最简单的方法是使用ws流操纵符。只需要:

inFile >> ws;

你也可以用这个代替你的peek循环。

将字符数组替换为字符串以获得更真实的c++体验。这也需要用getline free函数替换ifstream::getline。作为奖励,您的代码将适用于长度超过19个字符的名称。

    std::string tempFirst, tempLast, tempCourse;
    ...
    for (int i=0; i < numStudents; ++i) {
        inFile >> std::ws;
        getline(inFile, last, ',');
        inFile >> std::ws;
        getline(inFile, first, 'n');
        ...

在移动到下一行之前跳过当前行的其余部分

   inFile >> numStudents;
    std::string line;
    std::getline(inFile, line);//read the rest of the first line, you should do this before you start to read next line
    for (int i = 0; i < numStudents; i++)
    {
        std::getline(inFile, line); //line contains first name and last name
        size_t pos = line.find(",");
        std::cout << line.substr(0, pos) << std::endl //first name
            << line.substr(pos + 2) << std::endl; //last name,skip ", "
        std::getline(inFile, line); // course name and grades
        //you could split the course name and grades now with line
        std::cout << line << std::endl;
    }