简体   繁体   English

将数据从文件读入数组

[英]Reading data from file into array

I am trying to read specific data from a file into two 2D arrays. 我试图将文件中的特定数据读入两个2D数组。 The first line of data defines the size of each array so when I fill the first Array i need to skip that line. 第一行数据定义了每个数组的大小,所以当我填充第一个数组时,我需要跳过该行。 After skipping the first line, the first array fills with data from the file until the 7th line in the file. 跳过第一行后,第一个数组将填充文件中的数据,直到文件中的第7行。 The second array is filled with the rest of the data from the file. 第二个数组填充了文件中的其余数据。

Here's a labeled image of my data file: 这是我的数据文件的标记图像: 在此输入图像描述

and here's my (flawed) code so far: 到目前为止,这是我的(有缺陷的)代码:

#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;
}

Thanks for the input everyone. 感谢大家的意见和建议。

Your while loops iterate until the end of file, you don't need them. 你的while循环迭代直到文件结束,你不需要它们。

while (inFile >> seat) // This reads until the end of the plane.

Use instead (without the while ): 改为使用(没有while ):

for (int a = 0; a < FC_Row; a++)         // Read this amount of rows.
     for (int b = 0; b < FC_Col; b++)    // Read this amount of columns.
         inFile >> firstClass[a][b] ;    // Reading the next seat here.

Apply the same for economic seats. 适用于经济舱座位。


Also you might want change arrays into vectors, since variable size arrays are hell. 您也可能希望将数组更改为向量,因为可变大小的数组是地狱。

vector<vector<int> > firstClass(FC_Row, vector<int>(FC_Col)) ;
vector<vector<int> > economyClass(EconRow, vector<int>(EconCol)) ;

You need to #include <vector> to use vectors, their access is identical to arrays. 你需要#include <vector>来使用向量,它们的访问权限与数组相同。

You need to change the order of the for loops and reading from the file: 您需要更改for循环的顺序并从文件中读取:

for (rows = 0; rows < total_rows; ++ rows)
{
  for (col = 0; columns < total_columns; ++cols)
  {
    input_file >> Economy_Seats[row][column];
  }
}

I'll leave checking for EOF and handling of invalid input to the reader. 我会留下检查EOF并处理读者的无效输入。

You're reading into seat once then filling the array with this value. 您正在阅读seat ,然后使用此值填充数组。 Then you're reading into seat again, and filling the entire array with this new value. 然后你再次读入seat ,并用这个新值填充整个数组。

Try this: 试试这个:

int CurRow = 0;
int CurCol = 0;
while ( (inFile >> seat) && (CurRow < FC_Row)) {
  firstClass[CurRow][CurCol] = seat;
  ++CurCol;
  if (CurCol == FC_Col) {
    ++CurRow;
    CurCol = 0;
  }
}
if (CurRow != FC_Row) {
  // Didn't finish reading, inFile >> seat must have failed.
}

Your second loop should use economyClass not firstClass 你的第二个循环应当使用economyClass没有firstClass

The reason for switching the loop around like this is error handling, which is simplified when the loop exits upon error. 像这样切换循环的原因是错误处理,当循环在出错时退出。 Alternatively you could keep the for loops, use infile >> seat in the inner loop, but you'd then have to break out of two loops if reading failed. 或者你可以保留for循环,在内循环中使用infile >> seat ,但是如果读取失败,你必须打破两个循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM