2014-10-01 54 views
-1

例如,我在一個文件中讀入,其中包含;我很困惑將數據從文件寫入一維數組

12354343 12 12 35 87 48 100 65 435 
45395893 23 12 65 45 23 098 44 233 
12902440 23 09 20 04 40 054 00 100 

如何將第一行放在第一個元素中,第二行放在第二個元素中等等?

我已經選擇了代碼只是做了OPENFILE檢查,以確保我的文件被打開,它是 ,但我不知道怎麼回事,其餘

using namespace std; 
#include <cstdlib> 
#include <iostream> 
#include <fstream> 
#include <iomanip> 
int main() 
{ 
const int data = 50; 
int ray1[data]; 

int Id,score1,score2,score3,score4,score5,score6,score7,score8,mid,lab,codelab,finalT; 
cout << "Stdnt Id ----- Assignments ----- Mi Ex CL Fin" << endl; 
cout << "________ _______________________ __ __ __ ___" << endl; 
ifstream inFile; 
inFile.open("assign1Input.txt"); 
if (inFile) 
{ 
    while (inFile >> Id) 
    { 
     for(Id = 0; Id < 50 ; Id++) 
     { 
     cout << Id << setw (2) << score1 << setw (1) << score2 << setw (1) << score3 
     << setw (1) << score4 << setw (1) << score5 << setw (1) << score6 << setw (1) << score7 
     << setw (1) << score8 << setw (2) << mid << setw (2) << lab << setw (2) << codelab 
     << setw (2) << finalT << endl; 
     } 
    } 
    inFile.close(); 
} 
else 
{ 
    cout << "ERROR" << endl; 
} 

return 0; 
} 
+0

你可以做'inFile >>數組[i]'。 – 5gon12eder 2014-10-01 23:35:00

+0

yeahh崩潰的codeblocks我必須是一個非常糟糕的程序員哈哈 – student109 2014-10-01 23:51:16

回答

1
vector< vector<int> > data; 
ifstream file; 
string line; 
while (getline(file, line)) 
{ 
    data.resize(data.size() + 1); 
    ostringstream ss(line); 
    int x; 
    while (ss >> x) 
    { 
     data.back().push_back(x); 
    } 
}