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.

Recommended Answers

All 2 Replies

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

Thank you. Wow do I feel like an idiot, I can't believe it was that easy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.