How to not overwrite the file, C++ logging -


here code. everytime "save", overwrites old txt file. how can output same file new line or atlas new file. dynamic array , i'm using switch cases. after entering data, want save text file. , load in next time. load function 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 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 saved data press '6'" << endl;     cout << "to exit press '7'" << endl;     cout << endl;     cin>>userchoice; switch(userchoice) { case 1:         cout<<"enter number -->";         cin>>p1[size];         counter++;         size++;         break; case 2:         int udelete;         cout<<"enter number delete --> ";         cin>>udelete;         for(position = 0; position<size; position++)         {             if (p1[position] == udelete)                 break;         }         if(position>size)         {             cout<<"the number not in memory ";             cout<<endl;                 break;         }         for(i = position; i<size; i++){             p1[i]=p1[i+1];         }         size--;         cout<<"successfully deleted!!! ";         cout<<endl;         break; case 3:         (i=0; i<size; i++)                 {                     cout<<"your data" <<" " << << " " << "-->" <<p1[i]<<endl;         }         break; case 4:     {         int usearch;         cout<<"please enter figure 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");         (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;     } }     }} 

open file in "append" mode.

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

this create file if doesn't exist, , otherwise position write pointer @ end of file.

you don't have call open explicitly. there constructor you:

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

you don't need call close, since happen automatically when save destructed.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -