2010-10-12 34 views
0

作爲較大任務的一部分,我必須在可以讀取五行數據的類中創建方法,然後將該數據放入動態創建的對象中。我不知道如何將五行數據分開放入對象中。代碼應該有助於更好地解釋,但它不能按需要工作。如果有人能夠發現我的錯誤,請讓我知道。如果你能幫助我,將不勝感激。同樣每讀完五行,我都會創建一個新對象,直到沒有剩下行。我如何知道是否還有線?感謝您的幫助,我們將再次爲您提供幫助。幫助從文本文件中讀取每五行C++

inline void readFromFile(const string& fileName){ 

     string title; 
     string category; 
     unsigned int runningTime; 
     unsigned int yearOfRelease; 
     double price; 

     ifstream myReadFile; 
     myReadFile.open(fileName); 

     while(myReadFile) 
     { 
      myReadFile>>title; 
      myReadFile >> category; 
      myReadFile >> runningTime; 
      myReadFile >> yearOfRelease; 
      myReadFile >> price; 

      v.push_back(new DVD(title,category,runningTime,yearOfRelease,price)); 
     } 

     myReadFile.close(); 

     for(unsigned int i = 0; i < v.size(); i++){ 

      cout << *v.at(i) << endl; 

     } 

    } 

回答

2

問題是操作符>>用一個字符串只讀取一個字(不是一行)。

您需要使用std :: getline()函數。

 std::getline(myReadFile, title); 
     std::getline(myReadFile, category); 
     std::getline(myReadFile, runningTime); 
     std::getline(myReadFile, yearOfRelease); 
     std::getline(myReadFile, price); 

爲方便起見,你應該寫操作>>對DVD

std::istream& operator>>(std::istream& str, DVD& data) 
{ 
    // Read data into data here 
    return str; 
} 

現在你的循環變得輕鬆了許多寫:

std::copy(std::istream_iterator<DVD>(myReadFile), 
      std::istream_iterator<DVD>(), 
      std::back_inserter(v) 
     ); 
+0

這是我到目前爲止。它確實可能更容易,但我對C++的經驗非常有限,所以我試圖儘可能保持基本。我做了前幾個更改,但現在文件沒有被讀取,並且數據沒有被顯示。請原諒我在C++方面的知識不足,但我正在盡力學習。在代碼中我應該做出什麼樣的改變。 – Delanoy 2010-10-12 05:10:33

+0

@Delanoy:正如Martin所建議的,你應該使用'getline'而不是'>>'作爲字符串(在你的情況下''title'和'category')。其餘的字段是數字值,你可以繼續使用'>>'。 – casablanca 2010-10-12 05:17:05

+0

@casablanca:@Dalanoy:通常我會同意卡薩布蘭卡並建議使用操作符>>。但是文件格式清楚地表明一條記錄是五行。因此,您應該嘗試並保證您閱讀完整的五行。如果您使用>>操作符,尾部空格可能會留在最後一行。閱讀下一個對象時可能會出現問題。因此我會將五行讀入對象。然後,我會將每行轉換爲適當的類型(可能使用boost :: lexical_cast <>或第二個選項std :: stringstream) – 2010-10-12 05:44:53

0

這就是我現在的有文件仍然沒有被讀取。我迷失在做什麼。

inline void readFromFile(const string& fileName){ 

      string title; 
      string category; 
      unsigned int runningTime; 
      unsigned int yearOfRelease; 
      double price; 

      ifstream myReadFile; 
      myReadFile.open(fileName); 

      while(! myReadFile.eof()) 
      { 
       getline(myReadFile, title); 
       getline(myReadFile, category); 
       myReadFile >> runningTime; 
       myReadFile >> yearOfRelease; 
       myReadFile >> price; 

       v.push_back(new DVD(title,category,runningTime,yearOfRelease,price)); 
      } 

      myReadFile.close(); 

      for(unsigned int i = 0; i < v.size(); i++){ 

       cout << *v.at(i) << endl; 

      } 

     } 
0

我對getline()的使用略有困惑。嘗試通過調用myReadFile變量上的getline()來獲取數據。這使用了一個字符數組。因此,重寫代碼的一種可能方式可能如下:

PS:請注意,創建字符串時,您的文件名正確地劃定了斜線(\)。

void readFromFile(const string& fileName){ 
      char title[80]; 
      char category[80]; 
      unsigned int runningTime; 
      unsigned int yearOfRelease; 
      double price; 

      ifstream myReadFile; 
      myReadFile.open(fileName.c_str()); 

      while(! myReadFile.eof()) 
      { 
       myReadFile.getline(title, 80); 
       myReadFile.getline(category, 80); 
       myReadFile >> runningTime; 
       myReadFile >> yearOfRelease; 
       myReadFile >> price; 

       v.push_back(new DVD(title,category,runningTime,yearOfRelease,price)); 
      } 

      myReadFile.close(); 

      for(unsigned int i = 0; i < v.size(); i++){ 

       cout << *v.at(i) << endl; 

      } 

     } 
+0

有一個使用字符串的getline(),因此您不需要指定限制在線的大小。如果價格後面的最後一行有空格,會發生什麼情況? – 2010-10-12 12:55:49

+0

好點。我之前沒有發現std :: getline(istrm,string&)。 while循環也不是必需的,因爲文件只能有5行。 – Jughead 2010-10-13 03:53:43

+0

我得到它的工作,感謝大家的幫助! – Delanoy 2010-10-13 05:21:28