2015-05-17 124 views
-1

我有一個文本文件,它看起來像這樣:文件處理

A   4  6 
B   5  7 
c   4  8 

我想將它存放在三個不同的陣列:

char pro[];  // contains values A B C 
int arrival[]; // contains values of 4 5 5 
int Burst[]; // contains values of 6 7 8 

我不知道該怎麼辦那。任何幫助(教程,僞代碼等)表示讚賞。

+0

如果行數是已知的,是一些常量,那麼實際上你可以使用數組。否則,你應該使用一些標準容器,例如std :: vector。 –

+0

僞代碼:1.從文件中獲取數據2.將其存儲在數組中。哈哈jk,你需要使用文件io打開該文件。然後從該文件中讀取(您也可以在循環中重複使用文件>> ch,file >> int1,file >> int2,並將這些變量存儲到數組/向量中 –

+0

向我們展示迄今爲止已嘗試的內容。 – TobiMcNamobi

回答

0

這是我的建議。希望有所幫助。

基本上它做了什麼,它創建了三個容器(參見STL的std :: vector)。他們可以有無限大小(你不必宣佈它是前面的)。創建三個輔助變量,然後使用它們從流中讀取數據。

while循環中的條件檢查數據是否仍在文件流中。 我沒有使用.eof()方法,因爲它總是讀取文件中的最後一行兩次。

using namespaces std; 
 

 
int main() { 
 

 
vector<char> pro; 
 
vector<int> arrival; 
 
vector<int> Burst; 
 

 
/* your code here */ 
 

 
char temp; 
 
int number1, number2; 
 

 

 
ifstream file; 
 
file.open("text.txt"); 
 

 
if(!(file.is_open())) 
 
    exit(EXIT_FAILURE); 
 

 
while (file >> temp >> number1 >> number2) 
 
{ 
 
    pro.push_back(temp); 
 
    arrival.push_back(number1); 
 
    Burst.push_back(number2); 
 
} 
 

 
file.close(); 
 

 
}