ifstream不规则图案

时间:2011-11-28 06:40:15

标签: c++ file-io

我无法确定文件输入错误的位置。这是代码:

  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 << "\n\n";
}

void HistoryStudent::SetGrades(ifstream& inFile)
{
  inFile >> paper >> midterm >> final;
  cout << paper << " " << midterm << " " << final << "\n\n";
}

void MathStudent::SetGrades(ifstream& inFile)
{
  inFile >> quiz1 >> quiz2 >> quiz3 >> quiz4 >> quiz5
         >> test1 >> test2 >> final;
  cout << quiz1 << " "<< quiz2 << " " << quiz3 << " " << quiz4 << " " << quiz5
       << " " << test1 << " " << test2 << " " << final << "\n\n";
}

这是我从以下位置加载信息的文件:

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

我缺少大多数姓氏,对于第一个学生,名字有一个结尾。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

并不是说名字最后有一个换行符,而是姓氏在开头有换行符。从输入中读取int时,遇到标记int结尾的任何空格都会留在输入流中。

要解决此问题,请在读取姓氏之前,在SetGrades方法中或在循环结束时删除空格。后两者还需要在阅读numStudents后删除空格。删除空格的最简单方法是使用ws stream manipulator。所需要的只是:

inFile >> ws;

您也可以用此替换peek循环。

用字符串替换字符数组,以获得更真实的C ++体验。这也需要用ifstream::getline免费功能替换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');
        ...

答案 1 :(得分:1)

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

   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;
    }