Write in file in file_makeacc() does overwriting to the file even with ios::app

#include<iostream>
#include<fstream>
#include "conio.h" //GCC conio.h
#include<iomanip>
#include<windows.h>

using namespace std;

/* **************************************************
CLASSES
***************************************************** */

class Account
{
public:
int acno;
char name[20];
int amt;
char type;


void makeacc(int ac)
{
acno=ac;
cin.ignore();
cout<<"Enter Name : ";
cin.getline(name,20);
cout<<"Enter Account Type [(C)urrent||(S)avings] : ";
cin>>type;
type=toupper(type);
X:
cout<<"Enter Amount to be deposited [Current(>5000)||Saving(>1000)] :";
cin>>amt;
if(amt<5000)
{
if(type=='C')
{
cout<<"Error . Minimum amount = 5000 \n\n";
cin.ignore();
goto X;
}
else if(type=='S'&& amt<1000)
{
cout<<"Error . Minimum amount = 1000 \n\n";
cin.ignore();
goto X;
}

}
system("cls");

}

void modify(int acno)
{
int flag=0;
char ch,ch2;
cout<<"The Selected account is "<<acno;
cout<<"\nDo you want to modify this account? (Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cin.ignore();
cout<<"Update Name ? (Y/N)";
cin>>ch2;
if(ch2=='y'||ch2=='Y')
{
cin.ignore();
cout<<"Enter new name : ";
cin.getline(name,20);
flag=1;

}
cout<<"Update Account Type? (Y/N)";
cin>>ch2;
if(ch2=='y'||ch2=='Y')
{
cout<<"Enter new account type : ";
cin>>type;
type=toupper(type);
flag=1;

}

}


else if(flag==0 && ch=='n' || ch=='N')
{

cout<<"Nothing has been modified. Exiting...Press Any Key... ";
getch();
}
system("cls");
}
void draw(int x)
{
amt-=x;
}
void add(int x)
{
amt-=x;
}
int retacc()
{
return acno;
}
void showacc()
{
cout<<"\n\nAccount Number : ";
cout<<acno;

cout<<"\n\nName : ";
cout<<name;
cout<<"\n\nAccount Type [(C)urrent||(S)avings] : ";

cout<<type;
cout<<"\n\nAmount Deposited : ";
cout<<amt;
}
};
/**END OF CLASS**/

/****function Declarations*****/
void file_makeacc(int);
void file_del(int);
void file_modify(int);
void file_draw(int);
void file_display(int);
void file_deposit(int);
/***End***/


int main()
{
int n,num;


cout<<"\t\t\t\t\tBankersDelight V6.9";

do{

cout<<"\n\n\n1.ADD ACCOUNT ";
cout<<"\n\n2.CLOSE ACCOUNT ";
cout<<"\n\n3.MODIFY ACCOUNT ";
cout<<"\n\n4.WITHDRAWAL ";
cout<<"\n\n5.DEPOSIT ";
cout<<"\n\n6.BALANCE INQUIRY ";
cout<<"\n\n7.EXIT \n";
cout<<"\n\nEnter Choice (1-7) : ";
cin>>n;
system("cls");
switch(n)
{
case 1:
cout<<"\n\nEnter the A/C no : ";
cin>>num;
file_makeacc(num);
break;
case 2:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_del(num);
break;
case 3:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_modify(num);
break;
case 4:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_draw(num);
break;
case 5:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_deposit(num);
break;
case 6:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_display(num);
case 7:
cout<<"\n\n\tThanks for working with BankersDelightV6.9";
exit(0);

default :cout<<"\nInvalid Input...";
getch();
}

}while(n<7);
return 0;
}

/***********************
FUNCTIONS
***********************/

void file_makeacc(int n)
{
int flag=0;
Account ac;
fstream file,File;
file.open("Accounts.txt",ios::out|ios::binary);
cout<<file.peek();
if(file.peek()!=-1)
{ cout<<"hi";

while(!file.eof())
{

file.read((char *)&ac , sizeof (Account));
cout<<ac.retacc();
if(ac.retacc()==n)
{
cout<<"Same Account Exists ... Re-Enter A/C number! ";
flag=1;
}

}

}
file.close();
if(flag==0)
{
File.open("Accounts.txt",ios::app|ios::in|ios::binary);
ac.makeacc(n);
File.write((char *)&ac , sizeof (Account));
File.close();
}

}

void file_modify(int n)
{
bool flag=false;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof()&&flag==false)
{
file.read((char *)&ac , sizeof (Account));

if(ac.retacc()==n)
{
ac.showacc();
getch() ;
cout<<"\n\nEnter New Details\n\n";
ac.modify(n) ;
int pos=-1*(int)(sizeof(Account));
file.seekp(pos,ios::cur);
file.write((char *) &ac, sizeof(Account));
cout<<"\n\nRecord Updated";
flag=true;

}
}
file.close();
if(flag==false)
cout<<"\tAccount not found...";
getch();
system("cls");
}
void file_del(int n)
{
Account ac;
fstream file,file2;
file.open("Accounts.txt",ios::in|ios::binary);
file2.open("Temp.txt",ios::out|ios::binary);

if(!file)
{
cout<<"No Accounts Found! ";
getch();
}
while(!file.eof())
{
file.read((char *)&ac,sizeof(Account));

if(ac.retacc()!=n)
{
file2.write((char *) &ac, sizeof(Account));
}
}
file.close();
file2.close();
remove("Accounts.txt");
rename("Temp.txt","Accounts.txt");
Sleep(1000);
cout<<"Account has been removed! ";
getch();
system("cls");
}
void file_draw(int n)
{
int amt;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof())
{
if(ac.retacc()==n)
{
ac.showacc();
cout<<"Enter the amount to be withdrawn : ";
cin>>amt;
ac.draw(amt);
cout<<"Success!";
getch();
system("cls");
}
}
file.close();
}
void file_deposit(int n)
{
int amt;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof())
{
if(ac.retacc()==n)
{
ac.showacc();
cout<<"Enter the amount to be withdrawn : ";
cin>>amt;
ac.add(amt);
getch();
system("cls");
}
}
file.close();
}
void file_display(int n)
{
Account ac;
int flag=0;
fstream file;
file.open("Accounts.txt",ios::binary|ios::out);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}
cout<<"\nBALANCE DETAILS\n";

while(file.read((char *)&ac, sizeof(Account)))
{
if(ac.retacc()==n)
{
ac.showacc();
flag=1;
getch();
system("cls");
}
}
file.close();
if(flag==0)
cout<<"\n\nAccount number does not exist!";
}
/******************END OF PROJECT*************************/


Edit: Seems like using ios::in Solved it , i don't know why, would be grateful if someone told me why.
int flag=0;
Account ac;
fstream file,File;
file.open("Accounts.txt",*ios::in*|ios::out|ios::binary);
Hi,

when you use fstream you have to define in and out (if you plan on using both simultaneously)

if you are just inputting you may use ifstream and needless to say this goes for outputting too..

just interested which version of gcc are you using?

PS: please use code tags for future posts

http://www.cplusplus.com/articles/jEywvCM9/

TDM GCC 4.9.2 64bit release
Anyways. If you look i close the file after i opened it just for out i.e file.read...
So why do i need to do both as i open the file again as ios::in when i write
wait! let me put your code in code tags for my understanding

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include<iostream>
#include<fstream>
#include "conio.h" //GCC conio.h 
#include<iomanip>
#include<windows.h>

using namespace std;

/* **************************************************
CLASSES
***************************************************** */

class Account
{
public:
int acno;
char name[20];
int amt;
char type;


void makeacc(int ac)
{
acno=ac;
cin.ignore();
cout<<"Enter Name : ";
cin.getline(name,20);
cout<<"Enter Account Type [(C)urrent||(S)avings] : ";
cin>>type;
type=toupper(type);
X:
cout<<"Enter Amount to be deposited [Current(>5000)||Saving(>1000)] :";
cin>>amt;
if(amt<5000)
{
if(type=='C')
{
cout<<"Error . Minimum amount = 5000 \n\n";
cin.ignore();
goto X;
}
else if(type=='S'&& amt<1000)
{
cout<<"Error . Minimum amount = 1000 \n\n";
cin.ignore();
goto X;
}

}
system("cls");

}

void modify(int acno)
{
int flag=0;
char ch,ch2;
cout<<"The Selected account is "<<acno;
cout<<"\nDo you want to modify this account? (Y/N)";
cin>>ch;
if(ch=='y'||ch=='Y')
{
cin.ignore();
cout<<"Update Name ? (Y/N)";
cin>>ch2;	
if(ch2=='y'||ch2=='Y')
{	
cin.ignore();
cout<<"Enter new name : ";	
cin.getline(name,20);
flag=1;

}
cout<<"Update Account Type? (Y/N)";
cin>>ch2;
if(ch2=='y'||ch2=='Y')
{
cout<<"Enter new account type : ";
cin>>type;
type=toupper(type);
flag=1;

}

}


else if(flag==0 && ch=='n' || ch=='N')
{

cout<<"Nothing has been modified. Exiting...Press Any Key... ";
getch();	
} 
system("cls");
}
void draw(int x)
{
amt-=x;	
}
void add(int x)
{
amt-=x;	
}
int retacc()
{
return acno;
}
void showacc()
{
cout<<"\n\nAccount Number : ";
cout<<acno;

cout<<"\n\nName : ";
cout<<name;
cout<<"\n\nAccount Type [(C)urrent||(S)avings] : ";

cout<<type;
cout<<"\n\nAmount Deposited : ";
cout<<amt;	
}
};
/**END OF CLASS**/

/****function Declarations*****/
void file_makeacc(int);
void file_del(int);
void file_modify(int);
void file_draw(int);
void file_display(int);
void file_deposit(int);
/***End***/


int main()
{	
int n,num;


cout<<"\t\t\t\t\tBankersDelight V6.9";

do{

cout<<"\n\n\n1.ADD ACCOUNT ";
cout<<"\n\n2.CLOSE ACCOUNT ";
cout<<"\n\n3.MODIFY ACCOUNT ";
cout<<"\n\n4.WITHDRAWAL ";
cout<<"\n\n5.DEPOSIT ";
cout<<"\n\n6.BALANCE INQUIRY ";
cout<<"\n\n7.EXIT \n";
cout<<"\n\nEnter Choice (1-7) : ";
cin>>n;
system("cls");
switch(n)
{
case 1:	
cout<<"\n\nEnter the A/C no : ";
cin>>num;
file_makeacc(num);
break;
case 2:
cout<<"\n\n\tEnter the A/C no. : "; 
cin>>num;
file_del(num);
break;
case 3:
cout<<"\n\n\tEnter the A/C no. : "; 
cin>>num;
file_modify(num);
break;
case 4:
cout<<"\n\n\tEnter the A/C no. : "; 
cin>>num;
file_draw(num);
break;
case 5:
cout<<"\n\n\tEnter the A/C no. : "; 
cin>>num;
file_deposit(num);
break;
case 6:
cout<<"\n\n\tEnter the A/C no. : ";
cin>>num;
file_display(num);
case 7:
cout<<"\n\n\tThanks for working with BankersDelightV6.9";
exit(0);

default :cout<<"\nInvalid Input...";
getch();
}

}while(n<7);
return 0;
}

/***********************
FUNCTIONS
***********************/

void file_makeacc(int n)
{
int flag=0;
Account ac;
fstream file,File;
file.open("Accounts.txt",ios::out|ios::binary);
cout<<file.peek();
if(file.peek()!=-1) 
{	cout<<"hi";

while(!file.eof())
{

file.read((char *)&ac , sizeof (Account));
cout<<ac.retacc();
if(ac.retacc()==n)	
{
cout<<"Same Account Exists ... Re-Enter A/C number! ";
flag=1; 
}

}

}
file.close();
if(flag==0)
{
File.open("Accounts.txt",ios::app|ios::in|ios::binary);
ac.makeacc(n);
File.write((char *)&ac , sizeof (Account));
File.close();
}

}

void file_modify(int n)
{	
bool flag=false;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::out|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof()&&flag==false)
{
file.read((char *)&ac , sizeof (Account));

if(ac.retacc()==n)
{
ac.showacc(); 
getch()	;	
cout<<"\n\nEnter New Details\n\n";
ac.modify(n) ; 
int pos=-1*(int)(sizeof(Account));
file.seekp(pos,ios::cur);
file.write((char *) &ac, sizeof(Account));
cout<<"\n\nRecord Updated";
flag=true;

}
}
file.close();
if(flag==false)
cout<<"\tAccount not found...";
getch();
system("cls");
}
void file_del(int n)
{
Account ac;
fstream file,file2;
file.open("Accounts.txt",ios::in|ios::binary);
file2.open("Temp.txt",ios::out|ios::binary);

if(!file)
{
cout<<"No Accounts Found! ";
getch();
}
while(!file.eof())
{
file.read((char *)&ac,sizeof(Account));

if(ac.retacc()!=n)
{
file2.write((char *) &ac, sizeof(Account));
}
}
file.close();
file2.close();	
remove("Accounts.txt");
rename("Temp.txt","Accounts.txt");
Sleep(1000);
cout<<"Account has been removed! ";
getch();
system("cls");
}
void file_draw(int n)
{
int amt;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof())	
{
if(ac.retacc()==n)
{
ac.showacc();
cout<<"Enter the amount to be withdrawn : ";
cin>>amt;
ac.draw(amt);	
cout<<"Success!";
getch();
system("cls");	
}
}
file.close();
}
void file_deposit(int n)
{
int amt;
Account ac;
fstream file;
file.open("Accounts.txt",ios::in|ios::binary);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}

while(!file.eof())	
{
if(ac.retacc()==n)
{
ac.showacc();
cout<<"Enter the amount to be withdrawn : ";
cin>>amt;
ac.add(amt);
getch();
system("cls");	
}
}
file.close();	
}
void file_display(int n)
{
Account ac;
int flag=0;
fstream file;
file.open("Accounts.txt",ios::binary|ios::out);
if(!file)
{
cout<<"No Accounts Found! ";
getch();
}	
cout<<"\nBALANCE DETAILS\n";

while(file.read((char *)&ac, sizeof(Account)))
{
if(ac.retacc()==n)
{
ac.showacc();
flag=1;
getch();
system("cls");
}
}
file.close();
if(flag==0)
cout<<"\n\nAccount number does not exist!";
}
/******************END OF PROJECT*************************/


could you now point the line number?
200

Please avoid other func i uploaded this when my program wasn't complete.
But line 200 , that func works fine. I just want to know why it works :P
You open an fstream with the ios::out flag, meaning it is an output stream. Then you do a peek() on it, which is an input stream function. Because you used an fstream, there is a peek() function available, so there won't be a compilation error. I'm not entirely sure what happens when you try to do a peek on an fstream with the ios::out flag set, but it's probably not good.

I would recommend using std::ifstream and std::ofstream whenever possible. There are occasions when you need an fstream (for both read and write), but the function at line 200 is not one of them.
Topic archived. No new replies allowed.