2017-09-23 127 views
-1

我正在学习如何使用fstreamdynamic array,该程序应该打印出txt file中的所有数据,并可以执行诸如排序,删除性别或计算平均值年级。 但事实证明,代码很混乱,数组很难访问。C++如何将文件读入动态数组并访问数组元素

这是用于练习的文本文件。 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

+0

[要将输出读取到'std :: setw'。](http://en.cppreference.com/w/cpp/io/manip/setw) – user4581301

+0

如果您的文件格式尚未修复,我建议使用json解析器,例如https://github.com/nlohmann/json – schorsch312

回答

3

看来你没有被仔细地跟踪你在哪里,在文件中。我不知道为什么array []对于这些类型的任务在流行时很流行,但注意到你缺少一个抽象级别:

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

它往往是用类型来跟踪这个非常有帮助:

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

struct student 
{ 
    // as above 
}; 

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

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

利用这一点,我们就可以解密文件工作等等。祝你好运!

+0

抱歉,编译器返回错误信息“无法将主题转换为主题*” load_one_subject(f,all.subjects [n]);我错过了什么? –

+0

对不起,我不小心做了一个指针而不是直接引用的参数。编程时,我们必须时刻注意事物的类型。 –