我想从一个文件读取特定数据到两个二维数组。第一行数据定义了每个数组的大小,所以当我填充第一个数组时,我需要跳过该行。在跳过第一行之后,第一个数组填充文件中的数据直到文件中的第7行。第二个数组用来自文件的其余数据填充。从文件读取数据到数组
这是我的数据文件的标记图像:
,这里是我的(有瑕疵)到目前为止的代码:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream inFile;
int FC_Row, FC_Col, EconRow, EconCol, seat;
inFile.open("Airplane.txt");
inFile >> FC_Row >> FC_Col >> EconRow >> EconCol;
int firstClass[FC_Row][FC_Col];
int economyClass[EconRow][EconCol];
// thanks junjanes
for (int a = 0; a < FC_Row; a++)
for (int b = 0; b < FC_Col; b++)
inFile >> firstClass[a][b] ;
for (int c = 0; c < EconRow; c++)
for (int d = 0; d < EconCol; d++)
inFile >> economyClass[c][d] ;
system("PAUSE");
return EXIT_SUCCESS;
}
感谢大家的输入。
'int firstClass [FC_Row] [FC_Col];'是一个VLA,它是C99,而不是C++。 *一些* C++编译器支持它,但对可移植性不利。 – Erik 2011-03-08 23:40:13
+1为你清晰的图解。 MSPaint从我那里获取+1 :-) – corsiKa 2011-03-08 23:44:56
+1提供您的程序的示例。 – 2011-03-08 23:46:16