TXT文件仅部分读取

时间:2014-04-08 01:35:31

标签: c++ oop

有人会介意看看我的代码并告诉我他们是否看到错误吗? for循环可能存在问题。据我所知,循环只读第一个学生(海绵宝宝)和他的专业,但不会超越那个学生。关于为什么的任何想法?

#include <iostream>
#include <iomanip>
#include "gradebook.h"
#include <fstream>
#include <cstring>
#include <string>

using namespace std;

void BuildTitle(){
cout <<endl;
cout << "Student      |--------------------------------Final    Final    Letter" <<endl; //47 to final exam
cout << "Name         |--------------------------------Exam     Average   Grade" <<endl; // 56 to average
cout << "----------------------------------------------------------------------" <<endl; // 65 to letter - 72 total

}

int main()
{
    string input_file; //name of file to read data from
    string output_file; //name of file to write data to
    ifstream inFile; //file stream objects
    ofstream outFile;
    string str;


    char fn[20]; //char array for student first name
    char ln[20]; // same for last name
    char subject[20]; // and for subject
    int listsize; // integer for the number of students in the list to determine list size

    int b_lab, b_test, b_test2, b_test3, b_final, t_part, t_mid, t_final, c1, c2, c3, c4, c5, c_test, c_final;
    //declarations to store the different grades in each subject

    cout << "Welcome to the Gradebook Program\n";
    cout << "Please enter the name of the file which holds grades. Please only use .txt files\n";
    cout << "Filename: ";
    cin >> input_file;
    cout << "\nOkay, now enter the name of the file you want to store grades in.\n";
    cout << "Filename: ";
    cin >> output_file;
    cout << endl;

    inFile.open(input_file.c_str()); //Open the files for reading and writing
    outFile.open(output_file.c_str());


    //If the file is not open
    if(!inFile){
        cout << "\nInput file not found or unable to be opened\n";

        return 0;
        };

    //If the file is successfully opened
    inFile >> listsize;

    //below: create a dynamic list for the students
    Student ** list = new Student* [listsize];

    //below: truncate through the .txt file to gather information

   for(int x = 0; x < listsize ; x++){
        inFile.getline(ln,20,','); //third parameter changes delimiter to comma from null character
        inFile.ignore(1);
        inFile.getline(fn,20);
        inFile.getline(subject,17);
        inFile.getline(ln,20, ',');

        int sub1= strcmp(subject,"Biology");
        int sub2= strcmp(subject, "Theater");
        int sub3= strcmp(subject, "Computer Science");

        cout << ln <<"," <<fn << endl;
        cout << subject << endl;
        //above cout statements are only to check to see if the loop is working correctly

   }

(这只是代码的一部分。它读取的.txt文件是:

    6
Squarepants, Spongebob
Computer Science 90 72 85 96 100 88 80 91 92
Finklebottom, Joe
Biology 85 90 78 85 89
Dipwart, Marvin
Theater 85 72 95
van Houten, Milhouse
Computer Science 45 57 26 79 54 52 60 71 63
Simpson, Homer J.
Theater 82 76 74
Cyrus, Miley
Biology 74 65 58 62 71

1 个答案:

答案 0 :(得分:0)

我看到了

    inFile.getline(subject,17);
    inFile.getline(ln,20, ','); // <----- reads numbers after subject name

旨在从

中读取数字部分
Computer Science 90 72 85 96 100 88 80 91 92

但这行没有','分隔符。所以你可能会得到 failbit (来自getline manual)并且不会再发生读取:

  

如果函数不提取任何字符,则设置failbit标志   一旦(n-1)个字符找不到分隔字符   已写入s。请注意,如果后面的字符   输入序列中的那些(n-1)个字符恰好是   分隔字符,它也被提取,而failbit标志则不是   set(提取的序列正好是n个字符)。