這是一種被稱爲grades.txt文本文件:如何從文本文件讀取不同類型的數據到多個數組? C++
John Sebastian 90 80 85 90 75 70 89 80 90 86 88 89 99 100
Brown Ford 40 60 80 85 60 90 100 80 90 83 81 89 97 90
我需要閱讀所有的數據,並把它們放在不同的陣列。前四個級別是考試,其次是測驗,第三個是家庭作業。
#include <iostream>
#include <fstream>
using namespace std;
void letter_grade(string names[][2], int exams[][4], int quiz[][5], int homeworks[][5], float grades[2][2]);
int main() {
ifstream Openfile("grades.txt");
string names[2][2];
int exams[4][4];
int quiz[5][5];
int homeworks[5][5];
if(Openfile.is_open()){
for(int i = 0 ; i<2 ; i++){
Openfile >> names[i][0];
}
for(int j = 0 ; j<2; j++){
Openfile >> names[j][1];
}
}
else{
cout << "File is not open." << endl;
}
if(Openfile.is_open()){
for(int x = 0 ; x<4 ; x++){
for(int y = 0; y<4 ; y++){
Openfile >> exams[x][y];
}
}
}
return 0;
}
所以陣列,我計劃將是這樣的:
names[0][0] = John
names[1][0] = Sebastian
names[0][1] = Brown
names[1][1] = Ford
等。但我無法做到這一點,代碼不斷閱讀考試結果並將其寫入名稱數組中。
然後,我將從數組中計算這些學生的成績,並將結果保存在不同的文件中,如果我可以從文本文件中讀取數據,我會這樣做。
讀取文件中(和寫入到陣列)明智行明智,不是列。 –
[Here](https://stackoverflow.com/questions/23047052/why-does-reading-a-record-struct-fields-from-stdistream-fail-and-how-can-i-fi)'sa number的建議如何處理。首先爲每條記錄提供一個數據結構。 – user0042