簡體   English   中英

幫助從文本文件C ++閱讀每五行

[英]Help reading every five lines from text file 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;

        }

    }

問題在於帶有字符串的運算符>>僅讀取一個單詞(而不是一行)。

您需要使用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)
         );

這就是我現在所擁有的,文件仍未被讀取。 我不知道該怎么辦。

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;

            }

        }

我對您使用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;

            }

        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM