file handling

Every time I write in file using ofstream it works fine except for one thing that when the file is re-opened after writing something and we try to add more data then previously added data is removed how to get rid of this.

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
30
31
struct abc
{
   char name[100]; 
   int a;     
};


int main()
{
    
    
   ofstream file;
   file.open("text.dat", ios::out | ios::binary);
   
   
   abc x;
   
   
   
   x.a = 2;   
   cout<<"Enter name ";
   cin>>x.name;   
   
   
   
   file.write((char*)&x,sizeof(struct abc));
   file.close();
    
 getch();
 return 0;   
}
Last edited on
You need to use the ios::app flag in order to to append to the end of the file.
Topic archived. No new replies allowed.