簡體   English   中英

從具有結構數據類型的輸入文件中讀取

[英]Reading from input files with struct data type

    struct studentInfo {
        string studentFname, studentLname;
        int testScore;
        char grade;
    }student[20];
    
    void inputs(studentInfo(&student)[20]) {
        ifstream openFile;
        openFile.open("Student.txt");
        for (int i = 0; i < 20; i++) {
            openFile >> student[i].studentFname;
            openFile >> student[i].studentLname;
            openFile >> student[i].testScore;
        }
        openFile.close();
    }



  Student.txt file
    Duckey Donald 85
    Goof Goofy 89
    Brave Balto 93
    Snow Smitn 93
    Alice Wonderful 89
    Samina Akthar 85
    Simba Green 95
    Donald Egger 90
    Brown Deer 86
    Johny Jackson 95
    Greg Gupta 75
    Samuel Happy 80
    Danny Arora 80
    Sleepy June 70
    Amy Cheng 83
    Shelly Malik 95
    Chelsea Tomek 95
    Angela Clodfelter 95
    Allison Nields 95
    Lance Norman 88

我正在嘗試將上面的 txt 文件讀入一個結構數組。 該文件將不會打開。 我確定該文件位於正確的文件夾中。 如果您需要更多代碼,請告訴我。 你能幫忙的話,我會很高興。

嘗試改用二進制文件(.bin 或 .dat); 這種類型的文件允許您將 memory 的整個塊(例如 arrays)和結構保存到文件中,而不僅僅是像 normal.txt 文件中的字符。 有了它,您可以實現類似這樣的東西來從二進制文件中讀取和寫入結構,您只需要將其調整為結構數組:此處的代碼示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM