0

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;
        
    }
    
}

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • Probably not the bug you're hunting right now, but you'll have to hunt it later: [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/q/5605125/4581301) – user4581301 Aug 09 '22 at 23:10
  • 1
    Can you explain what, and how, led you to believe that this is how you go about reading from this file, in this situation? Are you sure that this is what the format of your file should be? Based on the shown code it's highly likely that you misinterpreted the program description of this sample task in your textbook, or your homework assignment. Can you [edit] your question and add ***in plain text*** the formal specifications for your program? – Sam Varshavchik Aug 09 '22 at 23:11
  • 2
    I see no attempt to read the file after it has been written. You should take your best shot at solving the problem before asking a question. There isn't much we can do to help you fix a problem you haven't had yet. – user4581301 Aug 09 '22 at 23:16
  • FYI, you can eliminate the `this->` syntax by using a coding style where members and parameters have different names. For example, prefix members with "m_". Less typing means less chance of typos. – Thomas Matthews Aug 09 '22 at 23:53
  • DVDfile >> dvdTitle >> artistName >> yearPurchased >> dvdGenre; If I use this before cout<< this should work I guess. But loop is running infinite – Syed Irfan Ali Shah Aug 10 '22 at 12:08

1 Answers1

0

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.

Keith
  • 26
  • 2