2014-02-27 30 views
0

我试图在文件加载到数组后将其打印出来的内容。我得到的第一个条目打印,但其余的都输出为0.我觉得我需要把另一个循环的地方,但我不知道哪里会最好的工作。获取从C++中的文件加载的数组的输出

这里是我的代码:

#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. 

任何帮助非常感谢! (另外,如果你能告诉我如何摆脱围绕在打印数据的第一和最后一个条目的空行,那会是一个不错的奖金。)

干杯

+0

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

回答

1

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

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

这意味着你正在阅读的空行来planeArray[1].name,然后试图从线Boeing-747planeArray[1].id因为格式不匹配int将无法​​读取。此时,流将进入错误状态,之后不会再读取任何内容。

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

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

+0

作为一种快速解决方案,我删除了每段数据之间的空白行,但我仍然获得相同的输出。对于虚拟行,它是否仅仅是数组中的另一个附加项,并且是数据块之间的空行的getline? –

+0

@MichaelDelahorne在我的回答中增加了另一个问题。对于虚拟变量,我只是指'string s;函数getline(infile中,S);'。那么'''是虚拟的,因为你实际上并不需要它的任何价值。 – Nabla

1

存在空间您的空中客车的x坐标。这样

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

来自同一线读取两次(123 300) 所以你是不同步您正在阅读的文件的结构。

你有两个选择:

  • 确保您的语法是正确的(只是删除空间和前进)
  • 添加额外的检查,以确保你读一个名称或数字,...
+0

我的部分愚蠢的错误。我现在已经解决了这个问题,并添加了任何缺失的数据,但我仍然得到相同的输出。 –

+0

相同的后续项目之间的白线:要么添加一个额外的“getline”或从您的文本文件中删除它们 –

+0

我已经删除了空白行和输出仍然是由于某种原因相同。 –

相关问题