2017-09-23 43 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

對不起,我不小心做了一個指針而不是直接引用的參數。編程時,我們必須時刻注意事物的類型。 –