Delanoy
Delanoy

Reputation: 41

Help reading every five lines from text file c++

As part of a larger assignment I have to create a method in a class that can read five lines of data then put that data into a dynamically created object. I am not sure how to go about getting the five lines of data separately into the object. The code should help to explain better but it does not work as desired. If someone can spot my mistakes please let me know. If you can help me it would be greatly appreciated. Also every five lines read I create a new object, until no lines are left. How would I know if any lines are left? Once again any help would be appreciated thank - you for your time.

    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;

        }

    }

Upvotes: 0

Views: 675

Answers (3)

Jughead
Jughead

Reputation: 887

I am slightly confused by your usage of getline(). Try getting the data by calling getline() on the myReadFile variable. This uses a character array. So one possible way of rewriting the code may be as given below:

PS: Please note that your filename correctly delimits the slashes (\) while creating the string.

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;

            }

        }

Upvotes: 0

Delanoy
Delanoy

Reputation: 41

This is what I have now, the file is still not being read. I am at a lost of what to do.

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;

            }

        }

Upvotes: 0

Loki Astari
Loki Astari

Reputation: 264331

The problem is that operator >> with a string only reads one word (not a line).

You need to use the std::getline() function.

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

For convenience you should write an operator >> for DVD

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

Now your loop becomes a lot easier to write:

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

Upvotes: 2

Related Questions