【问题标题】:Getting output from array which has been loaded from file in C++从 C++ 文件中加载的数组获取输出
【发布时间】:2014-02-27 15:13:05
【问题描述】:

我正在尝试在将文件加载到数组后打印出文件的内容。我正在打印第一个条目,但其余的都输出为 0。我觉得我需要在某个地方放置另一个循环,但我不确定它在哪里工作得最好。

这是我的代码:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

using namespace std;

struct plane //strcutre of plane data to use
{
   string name;
    string direction;
    int id;
    int coordX;
    int coordY;
    int height;
    int speed;

};

void populate (plane planeArray[], int n )
{
    string name, direction;
    int id, coordX, coordY, height, speed, i, c;
    ifstream infile;                              //declare input file stream
    infile.open("plane_data.txt");                //opens file
    if (!infile)
    {
        cout << "File cannot be reached";          //checks for invalid file path
    }

    for ( int i = 0; i < 4; i++ )
    {
        getline(infile, planeArray[i].name);
        infile >> planeArray[i].id;
        infile >> planeArray[i].coordX;
        infile >> planeArray[i].coordY;
        infile >> planeArray[i].height;
        infile >> planeArray[i].speed;
        getline(infile, planeArray[i].direction);
    }

    infile.close();

}

void text_display( plane planeArray[5])
{
    for ( int i = 0; i < 4; i++ )
    {
        cout << planeArray[i].name << endl;
        cout << planeArray[i].direction << endl;;
        cout << planeArray[i].id << endl;;
        cout << planeArray[i].coordX << endl;;
        cout << planeArray[i].coordY << endl;;
        cout << planeArray[i].height << endl;;
        cout << planeArray[i].speed << endl;;
        cout << endl;
    }
}


int main()
{
    const int N = 5;
    plane planeArray[N] = {};

    populate( planeArray, N );
    text_display( planeArray);
}

这是文件中包含的内容:

Airbus A380 
123456 
123 300 
25000 
400 
north

Boeing-747 
140
234567 
30000 
450 
north west

Cessna-404-Titan 
345678 
145 
29000 
400 
south

Sukhoi-Superjet-100 
456789 
120 
28000 
300 
south west

Lockheed-Jetstar 
567890 
270 
20000 
500 
east

这是我运行代码时得到的输出:

Airbus A380

123456
123
300
25000
400

north

0
0
0
0
0



0
0
0
0
0



0
0
0
0
0


Process returned 0 (0x0)   execution time : 0.090 s
Press any key to continue.

非常感谢任何帮助! (另外,如果你能告诉我如何去掉打印数据中第一个和最后一个条目周围的空白行,那将是一个不错的奖励。)

干杯

【问题讨论】:

  • 在每次循环迭代之间,您应该检查流是否处于错误状态;

标签: c++ arrays loops


【解决方案1】:

文件中的块之间有一个空行,您可以忽略它。

在读取第一个块并移动到第二个块后,文件位置仍然在空行的前面。然后,您对第二个块的读取与实际数据相差一行。

这意味着您正在向planeArray[1].name 读取一个空行,然后您尝试从Boeing-747 行读取到planeArray[1].id,这将失败,因为格式与int 不匹配。此时流进入错误状态,此后不再读取任何内容。

这可以通过在循环末尾添加一个额外的getline 到一个虚拟字符串来解决。

您的文件还缺少除第一个之外的所有块的第二个坐标,这将导致类似的问题。

【讨论】:

  • 作为快速修复,我删除了每个数据部分之间的空白行,但我仍然得到相同的输出。对于虚拟行,它是否只是对数组的另一个添加以及对数据块之间的空行的 getline?
  • @MichaelDelahorne 在我的回答中添加了另一个问题。对于虚拟变量,我的意思是string s; getline(infile,s);。那么s 是假的,因为你实际上不需要它的任何价值。
【解决方案2】:

在您的 x 坐标中有一个空间用于您的空中客车。这边

infile >> planeArray[i].coordX;
infile >> planeArray[i].coordY;

从同一行读取两次 (123 300) 所以你与你正在阅读的文件的结构不同步。

你有两个选择:

  • 确保您的语法正确(只需删除空格并继续)
  • 添加额外的检查以确保您阅读姓名或号码,....

【讨论】:

  • 我犯了一个愚蠢的错误。我现在已经修复了这个问题并添加了任何丢失的数据,但是我仍然得到相同的输出。
  • 后续项目之间的白线相同:添加额外的“getline”或从文本文件中删除它们
  • 我删除了空行,由于某种原因,输出仍然相同。
  • 在你的程序中你读到 1 个名字,5 个数字,1 个名字。在您的文本文件中,您只有 4 个连续数字。只需使您的文本文件完全符合您要阅读的内容
猜你喜欢
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多