C++ 不规则图案

C++ 不规则图案,c++,file-io,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++) { /

我很难准确指出我的文件输入到底哪里出错了。代码如下:

  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);
    }
然后是输出:

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

我遗漏了大多数姓氏,对于第一个学生,姓氏包含了一个结束行。我该如何解决这个问题呢?

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

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

inFile >> ws;
您也可以用此替换
peek
ing循环

用字符串替换字符数组,以获得更真实的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');
        ...
std::string tempFirst、tempLast、tempCourse;
...
对于(int i=0;i>std::ws;
getline(填充,最后一个“,”);
infle>>std::ws;
getline(填充,第一个“\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;
    }
infle>>numstudent;
std::字符串行;
std::getline(infle,line);//阅读第一行的其余部分,在开始阅读下一行之前应该这样做
对于(int i=0;istd::cout如何创建
list
和设置
list[i]
?特别是,当我为
列表
填写合适的定义和操作时,它打印了姓氏。
SetGrades
方法的参数最好是
istream
,这样它们会更通用。这将允许它们处理
cin
istringstream
>s、 允许您使用
std::string
?(因为我更喜欢
istream::getline()
)使用std::getline而不是std::istream::get,std::istream::peek。get和peek很难使用。它们会让你的代码很难看。在姓氏之前去掉空格会删除换行符,但程序仍然无法获得所有的姓氏。我将系统切换到字符串,它可以工作!getline比Infle好得多。谢谢你!
    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;
    }