2015-05-17 69 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(); 
 

 
}