2011-09-02 83 views
1

爲什麼這段代碼不想加載文本文件中的所有值?它只能正確加載第一個對象的項目,但之後它開始變得奇怪。下面是代碼:從文本文件中加載對象數據

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

using namespace std; 
class Location 
{ 
public: 
    Location() 
    { 
     locName = "No Name"; 
     locDesc = "No Description"; 
     locID = 0; 
    } 

    friend ostream& operator <<(ostream& outs, const Location & showMe) 
    { 
     // keeping output same as input... 
     outs << showMe.locName << endl; 
     outs << showMe.locDesc << endl; 
     outs << showMe.locID << endl; 

     return outs; 
    } 

    friend istream& operator >>(istream& ins, Location & inLoc) 
    { 
     getline(ins, inLoc.locName); 
     getline(ins, inLoc.locDesc); 
     ins >> inLoc.locID; 

     return ins; 
    } 

private: 
    string locName; 
    string locDesc; 
    int locID; 
}; 

int main() 
{ 
    ifstream inFile; 

    inFile.open("loc_data.txt"); 
    if (inFile.fail()) 
     { 
      cout << "\nCould not open the input file!"; 
      exit(1); 
     } 

    Location fileLoc[10]; 
    int i = 0; 

    while (inFile.good()) 
     { 
      inFile >> fileLoc[i]; 
      i++; 
     } 

    for (int j = 0; j < 10; j++) 
     cout << fileLoc[j]; 

    return 0; 
} 

輸入文件是:

Town Hall 
Main venue for functions 
1 
City Park 
Outdoor venue 
2 
Train Station 
Commuting point 
3 
Local Airport 
Long distance travel 
4 

,輸出是:

Town Hall 
Main venue for functions 
1 

City Park 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 
No Name 
No Description 
0 

我有一個suspucion即函數getline負責這一點,但不知道足以確定,但我真的很想知道爲什麼會發生這種情況,而不僅僅是「修復」。

+0

你有文件是什麼?你怎麼知道哪個字段在哪裏結束? – Jagannath

回答

2

你的第一個問題是,這行不提取換行符:

ins >> inLoc.locID; 

這意味着,在第二Locationgetline1後提取行的其餘部分 - 即一個空字符串 - 名字。

這意味着您很快就會與您想要閱讀的行不同步。

你也應該考慮改變你的while循環,因爲在你提取一個新位置之前,你檢查流是否「好」,但是你不檢查提取是否成功,你認爲提取是有效的。

+0

事實上,在ins >> inLoc.locID解決它之後,添加一個char temp變量,然後使用ins.get(temp)。 – georgelappies

+0

@georgelappies:或者你可以使用'ins.get()'或'ins.ignore()'來忽略一個字符或者'ins.ignore(std :: numeric_limits :: max(),' \ n')'忽略到下一個換行符的所有內容。 –

1

這工作:

#include <iostream> 
#include <fstream> 
#include <cstdlib> 
#include <sstream> 

using namespace std; 
class Location 
{ 
public: 
    Location() 
    { 
     locName = "No Name"; 
     locDesc = "No Description"; 
     locID = 0; 
    } 

    friend ostream& operator <<(ostream& outs, const Location & showMe) 
    { 
     // keeping output same as input... 
     outs << showMe.locName << endl; 
     outs << showMe.locDesc << endl; 
     outs << showMe.locID << endl; 

     return outs; 
    } 

    friend istream& operator >>(istream& ins, Location & inLoc) 
    { 
     getline(ins, inLoc.locName); 
     getline(ins, inLoc.locDesc); 
     std::stringstream ss; 
     std::string s; 
     getline(ins, s); 
     ss << s; 
     ss >> inLoc.locID; 

     return ins; 
    } 

private: 
    string locName; 
    string locDesc; 
    int locID; 
}; 

int main() 
{ 
    ifstream inFile; 

    inFile.open("loc_data.txt"); 
    if (inFile.fail()) 
     { 
      cout << "\nCould not open the input file!"; 
      exit(1); 
     } 

    Location fileLoc[10]; 
    int i = 0; 

    while (inFile.good()) 
     { 
      inFile >> fileLoc[i]; 
      i++; 
     } 

    for (int j = 0; j < 10; j++) 
     cout << fileLoc[j]; 

    return 0; 
} 
1

我相信這個問題是這一行:插件>> inLoc.locID;

問題是,當您使用>>操作符讀取此值時,它將在緩衝區中留下下一行字符,這會導致讀取循環結束(如果您逐步瀏覽它,可以看到)。

爲了解決這個問題,我認爲你需要故意從緩衝區中清空這個字符,或者使用getline,並將輸入(驗證我希望!)轉換爲字符串中的數字。

希望這有助於