我正在尝试将.txt文件读入我的程序

时间:2016-01-27 02:39:52

标签: c++ file

该文件具有每行必须阅读的不同内容。我正在使用指针数组创建成绩簿,但首先我必须阅读我的教师提供的这个文件。第一行(7)是班上学生人数。第二行(5)是他们拥有的任务数量。之后的行是学生标识符及其各自的分配分数。以下是我的程序必须阅读的.txt文件示例:

7 
5
asd251  98.50   75  100 0   77
a3t46   83.50   100 100 62.5    77
6t23d   76.30   85  0   69.5    81.5
3kjl233lk   88.50   90  0   97  104.5
23kjk23lk1  98.25   0   100 93  95.5
12j1    76.00   75  50  87.5    104
142jjll 45.75   80  100 71  0

我能够阅读前3行,但我无法阅读其余的内容。这是我到目前为止的代码:

#include <string> 
#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

int main()
{

    ifstream file;
    file.open("textfile.txt"); //name of file
    int numstud; //number of students
    int assignments = 0; //number of assignments
    string stud; //student identifier
    double a1, a2, a3, a4, a5; //each specific assignment

    while (file >> numstud >> assignments >> stud >> a1 >> a2 >> a3 >> a4 >>     a5)
    {
        cout << numstud << endl << assignments << endl << stud << " " << a1     << " " << a2 << " " << a3 << " " << a4 << " " << a5 << endl;
    }

    /*
    while (file >> assignments)
    {
        cout << assignments << endl;
    }

    while (file >> stud >> a1 >> a2 >> a3 >> a4 >> a5)
    {
        cout << stud << a1 << a1 << a3 << a4 << a5 << endl;
    }
    */
    /*
    while (file >> id >> studname >> age)
    {
        cout << id << studname << age << '\n';
    }

    */
    system("pause");
    return 0;
} 

0 个答案:

没有答案