Syed Irfan Ali Shah
Syed Irfan Ali Shah

Reputation: 11

How to read multiple records from a txt file in C++

I have created multiple records using filing, but I am not able to read the whole data from a file, what should I change in this code to print all the records? Here: dvdTitle, artistName, and dvdGenre are strings. While yearPurchased is of int type

void addDvd()
{
    
    cout << "Enter DVD Title\n";
    cin >> this->dvdTitle;
    cout << "Enter Artists Name\n";
    cin >> this->artistName;
    cout << "Enter Year Purchased\n";
    cin >> this->yearPurchased;
    cout << "Enter DVD Genre\n";
    cin >> this->dvdGenre;
    fstream DVDfile;
    DVDfile.open("DVD.txt", ios::app);
    DVDfile << "DVD Title: " << dvdTitle << endl << "Artist Name: "
            << artistName << endl << "Year Purchased: " << yearPurchased << endl
            << "DVD Genre: " << dvdGenre << endl << endl;
    
}
void displayDvd()
{
    fstream DVDfile;
    DVDfile.open("DVD.txt", ios::in);
    while (!DVDfile.eof())
    {
        cout << "DVD Title: " << dvdTitle << endl << "Artist Name: "
             << artistName << endl << "Year Purchased: " << yearPurchased
             << endl << "DVD Genre: " << dvdGenre << endl << endl;
        
    }
    
}

Upvotes: 0

Views: 283

Answers (1)

Keith
Keith

Reputation: 26

If you want to write to a file you must open it, write to it, and then close it.

fstream dvdFile;
dvdFile.open("DVD.txt", ios::app);
dvdFile << (this->dvdTitle); // parenthesis for clarity.
// file closes when it goes out of scope
// consider manually closing for error handling reasons
// dvdFile.close();

If you want to read from a file you must open it, read from it, and then close it.

fstream dvdFile;
dvdFile.open("DVD.txt", ios::in);
dvdFile >> (this->dvdTitle); // parenthesis for clarity.
// file closes when it goes out of scope.
// again consider manually closing it.

You're opening the file but then you're not actually reading from it. Also, this approach might work the first time, but what happens when you run the addDvd function twice? What happens when you write multiple strings to the file? I expect the file will look kind of funky and difficult to parse.

If your dvdTitle is "Toot Sweet" and the artistName is "Chitty Chitty Bang Bang", and then you write to the file:

dvdFile >> (this->dvdTitle);
dvdFile >> (this->artistName);
// or equivalently dvdFile >> (this->dvdTitle) >> (this->artistName);

The file's contents will look mashed together:

Toot SweetChitty Chitty Bang Bang

When you read back out from the file, the contents will also not be what you expected:

dvdFile >> (this->dvdTitle); // this->dvdTitle == "Toot"
dvdFile >> (this->artistName); // this->artistName == "SweetChitty"

You must come up with a way to separate your output so you can distinguish where one part of your data ends (the end of the title) and where another part begins (the artist's name). This is called a file format. Consider looking up CSV (comma separated value) files for an easy to see and debug example of a useful file format. You will also need a way to determine where one record ends and another begins.

Upvotes: 0

Related Questions