0

Here is my code. Everytime I "Save", it overwrites the old txt file. How can I output into the same file new line or atlas to a new file. This is a dynamic array and I'm using switch cases. After entering the data, i want to save it to a text file. and Load it back in the next time. The load function also works well.

#include<iostream>
#include<string>
#include<fstream> //to save file in text
using namespace std;
int main ()
{
int *p1;
int size=0;
int counter=0;
p1 = new int[size];
int userchoice;
int i;
int position;
while(1)
{
    cout << "Please enter your choice " << endl;
    cout<<endl;
    cout << "To insert Press '1'" << endl;
    cout << "To Delete press '2'" << endl;
    cout << "To View press '3'" << endl;
    cout << "To Search press '4'" << endl;
    cout << "To Save Press '5'" << endl;
    cout << "To Load Previously saved Data press '6'" << endl;
    cout << "To Exit press '7'" << endl;
    cout << endl;
    cin>>userchoice;
switch(userchoice)
{
case 1:
        cout<<"Enter a Number -->";
        cin>>p1[size];
        counter++;
        size++;
        break;
case 2:
        int udelete;
        cout<<"Enter a number to delete --> ";
        cin>>udelete;
        for(position = 0; position<size; position++)
        {
            if (p1[position] == udelete)
                break;
        }
        if(position>size)
        {
            cout<<"The number is not in the memory ";
            cout<<endl;
                break;
        }
        for(i = position; i<size; i++){
            p1[i]=p1[i+1];
        }
        size--;
        cout<<"Successfully Deleted!!! ";
        cout<<endl;
        break;
case 3:
        for (i=0; i<size; i++)
                {
                    cout<<"Your data" <<" " << i << " " << "-->" <<p1[i]<<endl;
        }
        break;
case 4:
    {
        int usearch;
        cout<<"Please enter the figure you would like to search ";
        cout<<"->";
        cin>>usearch;
            for(i=0; i>size; i++)
            {
                if (p1[size]==usearch)
                    break;
            }
             if(usearch==size)
             {
                cout<<"not found";
             }
             cout<<"Position at: "<<i+1<<endl;
             break;
    }
case 5:
    {
            ofstream save;
        save.open("data.txt");
        for (i=0; i<size; i++)
        {
            save <<p1[i] <<endl;
        }
        save.close();
        cout<<"File Saved "<<endl;
            break;
        }
case 6:
    {
    string read;
    ifstream file_("data.txt");
    if (file_.is_open())
    {
        while(getline(file_,read))
        {
            cout << read << "\n";
        }
        file_.close();
    }
    else
    cout << "File Not open" << endl;
    cin.get();
    break;
    }
case 7:
    {
    return 0;
    }
}
    }}
  • This can be also useful http://stackoverflow.com/questions/10071137/appending-a-new-line-in-a-filelog-file-in-c I think the answer is what you want. – nhzandi Jan 21 '16 at 07:49

2 Answers2

4

Open the file in "append" mode.

save.open( "data.txt", ofstream::out | ofstream::app );

This will create the file if it doesn't exist, and otherwise position the write pointer at the end of the file.

You don't have to call open explicitly. There is a constructor that will do it for you:

ofstream save( "data.txt", ofstream::out | ofstream::app );

You also don't need to call close, since that will happen automatically when save is destructed.

paddy
  • 60,864
  • 6
  • 61
  • 103
1

You may have two cases: 1) Append data at the end of file 2) Insert data in the middle of file

CASE I: Append data at the end of file: To append data at the end, you should open file in "APPEND" mode.

i.e. std::ofstream LogFile("TempFile.txt", std::ios_base::app | std::ios_base::out);

CASE II: Insert data in the middle of file: To Insert data in the middle of file,you can't use "APPEND" mode.Use other modes to open file and use seekp(offset,direction) which allows you to provide an offset and a direction. e.g

fstream LogFile("TempFile.txt", std::ios_base::binary | std::ios_base::out | std::ios_base::in);
    LogFile.seekp(10, ios::beg);
    LogFile.write("abc", 4);