0

I'm writing a program that will keep track of every Anime/Manga I watch/read(I'm an avid fan). I'm trying to make a database in order to keep track of all of the titles of the Anime/Manga. My problem is that when i try to add a new title to the existing list, it overwrites the entries that are already there. Here is the code for my add function.

int Add(void)
{
    outfile.open("database.txt");

    cout << "What is the title of the Anime/Manga that you wish to add?" << endl;
    cout << "::>";
    cin >> Title;

    outfile << Title << endl;

    outfile.close();
}

As I said it overwrites what was already in the file. Here is the function that reads the content of the file in case that might help.

int List(void)
{
    int count = 0;

    infile.open("database.txt");

    //Check for errors
    if(infile.fail())
    {
        cerr << "Could not open Database file.";
        exit(1);
    }

    while(!infile.eof())
    {
        infile >> Title;
        cout << Title << endl;
        count++;
    }

    //Display number of entries
    cout << count << " entries found." << endl;

    infile.close();
}

I have infile and outfile declared globally if that matters. Title is also global.

2
Contributors
2
Replies
19
Views
4 Years
Discussion Span
Last Post by Sci3nc3F1cti0n
2

Open the file in the mode that appends new data on the end instead of overwriting the existing data.

outfile.open("database.txt", ios::app);

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.