c ++如何将文件读入动态数组并访问数组元素

时间:2017-09-23 04:33:29

标签: c++ file pointers dynamic-arrays

我正在学习如何将fstreamdynamic array一起使用,该程序应打印txt file中的所有数据,并可执行排序,删除性别或计算平均成绩等操作。 但结果是代码很乱,阵列很难访问。

这是用于练习的文本文件。 enter image description here

代码:

struct studentData{
   string branch;
   string name;
   char gender;
   double grade;
   int number;
   int total;
};


int main() {

    int line_count = 0;
    ifstream file_in;
    int couter = 0;
    file_in.open("student.txt");
    if(!file_in.good())
        {
            cout << "Eror, could not open the file." << endl;
            file_in.clear();
            return -1;
        }   
        line_count = openFileTest(file_in);
        file_in.clear();
        file_in.seekg(ios::beg);
        studentData* p_studentData = new studentData[line_count];

    loadStudentData(file_in, p_studentData,couter);
    displayStudentData(p_studentData, line_count,couter);

        delete [] p_studentData;

        file_in.close();

        return 0;
}


int openFileTest(ifstream& file_in)
{
    string temp;
    int linecount = 0;

    while(getline(file_in, temp))
    {
        linecount ++;
    }

    return linecount;

}

void loadStudentData(ifstream& file_in,studentData* p_studentData,int couter)
{

    int temp ;
    file_in >> p_studentData -> total ;
    temp = p_studentData->total;

    for(int i=0;i<temp;i++)
    {
    file_in >> p_studentData->branch >> p_studentData->number ;
        for(int k=0;k<p_studentData[i].number;k++)
        {
            file_in >> p_studentData[k].name  >> p_studentData[k].gender >> p_studentData[k].grade ;
        }   
        p_studentData++;
    }
    return;
}

void displayStudentData(studentData* p_studentData, int count_line,int couter)
{

        cout << p_studentData->total << endl;
        int temp = p_studentData->total ;
    for(int i=0;i<temp;i++)
    {
        cout << p_studentData[i].branch << "  " ;
        cout << p_studentData[i].number << endl;
        for(int j=0;j<p_studentData[i].number;j++)
        {
            cout << p_studentData[j].name << "  " << p_studentData[j].gender << "  " << p_studentData[j].grade << endl;
        }
    }
    return;
}

我得到的输出是:

enter image description here

1 个答案:

答案 0 :(得分:3)

您似乎并不小心跟踪文件中的位置。我不知道为什么数组[]在向量的情况下如此受欢迎,但注意到你缺少抽象级别:

你的代码:学生阵列 赋值:主题数组;每个科目都是一群学生

使用类型来跟踪这一点通常非常有用:

struct student
{
  // as above
};

struct subject
{
  string name;
  int number_of_students;
  student* students;
};

struct all_subjects
{
  int number_of_subjects;
  subject* subjects;
};

使用它,我们可以解密文件:

all_subjects load_all_subjects()
{
  ifstream f( ... );
  ...
  all_subjects all;
  f >> all.number_of_subjects;
  all.subjects = new subject[ all.number_of_subjects ];
  for (int n = 0; n < all.number_of_subjects; n++)
    load_one_subject( f, all.subjects[ n ] );
  return all_students;
}

void load_one_subject( std::ifstream& f, subject& one_subject )
{
  f >> one_subject.name;
  f >> one_subject.number_of_students;
  one_subject.students = new student[ one_subject.number_of_students ];
  for (int n = 0; n < one_subject.number_of_students; n++)
    load_one_student( f, one_subject.students[ n ] );
}

等等。祝你好运!