C++ 将文件中的数据读入数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5239689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:44:30  来源:igfitidea点击:

Reading data from file into array

c++

提问by darko

I am trying to read specific data from a file into two 2D arrays. 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. The second array is filled with the rest of the data from the file.

我正在尝试将文件中的特定数据读取到两个二维数组中。第一行数据定义了每个数组的大小,因此当我填充第一个数组时,我需要跳过该行。跳过第一行后,第一个数组填充文件中的数据,直到文件中的第 7 行。第二个数组填充了文件中的其余数据。

Here's a labeled image of my data file: enter image description here

这是我的数据文件的标记图像: 在此处输入图片说明

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.

感谢大家的意见和建议。

采纳答案by Tugrul Ates

Your whileloops 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>使用向量,它们的访问与数组相同。

回答by Thomas Matthews

You need to change the order of the forloops 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 和处理无效输入给读者。

回答by Erik

You're reading into seatonce then filling the array with this value. Then you're reading into seatagain, and filling the entire array with this new value.

您正在阅读seat一次,然后用这个值填充数组。然后你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 economyClassnot firstClass

你的第二个循环应该使用economyClassnotfirstClass

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 >> seatin the inner loop, but you'd then have to break out of two loops if reading failed.

像这样切换循环的原因是错误处理,当循环因错误退出时简化了。或者,您可以保留 for 循环,infile >> seat在内部循环中使用,但如果读取失败,则必须中断两个循环。