Writing/Reading data to/from a file from a while loop

I am attempting to write data to a file from a while loop and then read the file back to the output screen. All three loops write to the file BUT it is only the last loop that reads back to the output screen (three times). Here is what I have written, maybe you can tell me where I am going wrong:

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void getSong(int & count, string & title, string & artist, string & genre);
void writeFile(ofstream & outFile, string & title, string & artist, string & genre);
void showSong(ifstream & inFile, string & title, string & artist, string & genre);

int main()
{
int count = 0;
string title;
string artist;
string genre;
ofstream outFile;
ifstream inFile;

outFile.open ("E:\\data.txt");
if (outFile.fail())
{
cout << "ERROR! File did not open. \n\n";
exit(1);
}

cout << "Enter 3 songs:\n";

for (int i = 0; i < 3; i++)
{
getSong(count, title, artist, genre);
writeFile(outFile, title, artist, genre);
}
outFile.close();

showSong(inFile, title, artist, genre);

return 0;
}

void getSong(int & count, string & title, string & artist, string & genre)
{
cout << "Title: ";
getline(cin, title);
cout << "Artist: ";
getline(cin, artist);
cout << "Genre: ";
getline(cin, genre);
cout << endl;
system("cls");
}

void writeFile(ofstream & outFile, string & title, string & artist, string & genre)
{
outFile << title << endl << artist << endl << genre << endl << endl;
}


void showSong(ifstream & inFile, string & title, string & artist, string & genre)
{
inFile.open("data.txt");
cout << "Here are your songs: \n";

while (!inFile.eof())
{
inFile >> title >> artist >> genre;
cout << endl << title << endl << artist << endl << genre << endl;

inFile >> title >> artist >> genre;
cout << endl << title << endl << artist << endl << genre << endl;

inFile >> title >> artist >> genre;
cout << endl << title << endl << artist << endl << genre << endl;
inFile.close();
}
}
Last edited on
You have some correct code, but the sequencing of various events needs to be re-arranged.

Here's part of the answer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
    int count = 0;
    string title;
    string artist;
    string genre;
    ofstream outFile;
    ifstream inFile;

    outFile.open ("data.txt");
    if (outFile.fail())
    {
        cout << "ERROR! File did not open. \n\n";
        exit(1);
    }

    cout << "Enter 3 songs:\n";

    for (int i = 0; i < 3; i++)
    {
        getSong(count, title, artist, genre);
        writeFile(outFile, title, artist, genre);
    }
    outFile.close();

    showSong(inFile, title, artist, genre);

    return 0;
}

I added various code into main(). It is also removed from the original functions.

The last part, showsong() I've not changed, it will still need some attention.
But at least the data file should look better.
Thanks for the prompt response. The data file still has the same results. And the output show the error message. Any other suggestions?
As I said previously, the code I added in main() has to be removed from the other functions:
1
2
3
4
5
6
7
8
9
10
11
void getSong(int & count, string & title, string & artist, string & genre)
{
        cout << "Title: ";
        getline(cin, title);
        cout << "Artist: ";
        getline(cin, artist);
        cout << "Genre: ";
        getline(cin, genre);
        cout << endl;
        system("cls");
}

1
2
3
4
void writeFile(ofstream & outFile, string & title, string & artist, string & genre)
{
    outFile << title << endl << artist << endl << genre << endl << endl;
}
the output show the error message.

Not sure what message you mean.
Perhaps I removed "E:\" from the filename as that's my cd-drive.
Last edited on
Thanks agian for the quick reponse. Below is what I have now. I am still getting the original output but all three loops are writing to the file. Can you advise me how to correct the inFile to read the entire document, rather than just the first three lines? Thanks so much again.

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void getSong(int & count, string & title, string & artist, string & genre);
void writeFile(ofstream & outFile, string & title, string & artist, string & genre);
void showSong(ifstream & inFile, string & title, string & artist, string & genre);

int main()
{
int count = 0;
string title;
string artist;
string genre;
ofstream outFile;
ifstream inFile;

outFile.open ("E:\\data.txt");
if (outFile.fail())
{
cout << "ERROR! File did not open. \n\n";
exit(1);
}

cout << "Enter 3 songs:\n";

for (int i = 0; i < 3; i++)
{
getSong(count, title, artist, genre);
writeFile(outFile, title, artist, genre);
}
outFile.close();

showSong(inFile, title, artist, genre);

return 0;
}

void getSong(int & count, string & title, string & artist, string & genre)
{
cout << "Title: ";
getline(cin, title);
cout << "Artist: ";
getline(cin, artist);
cout << "Genre: ";
getline(cin, genre);
cout << endl;
}

void writeFile(ofstream & outFile, string & title, string & artist, string & genre)
{
outFile << title << endl << artist << endl << genre << endl << endl;
}


void showSong(ifstream & inFile, string & title, string & artist, string & genre)
{
inFile.open("data.txt");
inFile >> title >> artist >> genre;
inFile.close();

cout << "Here are your songs: " << endl << title << endl << artist << endl << genre << endl;
}

May I ask, when you share your code, please use the code tags, using the <> button from the format options on the right. Like this [code]your code here[/code].

Reading from the file is going to be very similar in structure to the way it was created. That is:
open the file for input
read and display all of the data
close the file.


The difference is, we originally used a loop to output exactly three songs. When reading from the file, it's best to assume that there is an unknown quantity of songs. So we will just keep reading/displaying until there is no more data.
Hence we need something like this:
1
2
3
4
5
6
7
    while (readSong(inFile, title, artist, genre))
    {
        cout << "Title:  " << title << endl;
        cout << "Artist: " << artist << endl;
        cout << "Genre:  " << genre << endl;
        cout << endl;
    }


and the function readSong() something like this:
1
2
3
4
5
6
7
8
9
bool readSong(ifstream & inFile, string & title, string & artist, string & genre)
{
    string dummy;
    getline(inFile, title);
    getline(inFile, artist);
    getline(inFile, genre);
    getline(inFile, dummy); // get blank line
    return inFile.good();
}


Topic archived. No new replies allowed.