2014-02-27 28 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

我已經刪除了空白行和輸出仍然是由於某種原因相同。 –

相關問題