I made this program but its not working in append mode (ios::app is not working). Can someone tell me where am i going wrong. Thank you for your help in advance.
#include<fstream.h> //For ofstream and ifstream
#include<conio.h> //for getch()
#include<stdio.h> //for eof()
struct student //defined a structure student
{int rno;
int admno;
float tm;
char name[20];};
void getdata(student &); //function prototype for entering data
void readfile(); //function prototype for reading data
void searchfile(); //Function prototype for searching user entered roll no.
void writefile(); //function prototype for entering data
student s1;
void main()
{clrscr();
char ch1='y';
int ch;
while ((ch1=='y')||(ch1=='Y'))
{cout<<"1)Write into a file\n2)Read from existing file \n3)Search an existing record";
cout<<"\nEnter your choice : ";
cin>>ch;
switch(ch)
{case 1:
writefile();
break;
case 2:
readfile();
break;
case 3:
searchfile();
break;
default:
cout<<"\nPlease enter a valid choice";
break;}
cout<<"\nDo you want to continue (y/n) ? : ";
cin>>ch1;
cout<<endl;}
getch();
}
void writefile()
{ofstream f1;
f1.open("soumz.dat",ios::binary||ios::app);
getdata(s1);
f1.write((char*)&s1,sizeof(student));
f1.close();}
void readfile()
{clrscr();
ifstream f2;
f2.open("soumz.dat",ios::binary||ios::in);
while (!f2.eof())
{f2.read((char*)&s1,sizeof(student));
cout<<"\n\t\t\t Details of student are : \n";
cout<<"Name of student is : "<<s1.name;
cout<<"\nRoll no. is : "<<s1.rno;
cout<<"\nAdmission number is : "<<s1.admno;
cout<<"\nTotal marks achieved is : "<<s1.tm;
break;}
f2.close();}
void getdata(student &s)
{clrscr();
cout<<"Enter your name : ";
gets(s.name);
cout<<"\nEnter your admission number : ";
cin>>s.admno;
cout<<"\nEnter your roll no. : ";
cin>>s.rno;
cout<<"\nEnter total marks in 5 subjects : ";
cin>>s.tm;
cout<<"\nYour details have been saved ";
getch();
clrscr();}
void searchfile()
{clrscr();
int rn;
ifstream f3;
char ch='y',f='n';
f3.open("soumz.dat",ios::binary);
while((ch=='y')||(ch=='Y'))
{cout<<"\nEnter roll no. to search for : ";
cin>>rn;
while (!f3.eof())
{f3.read((char*)&s1,sizeof(student));
if (s1.rno==rn)
{f='y';
break;}}
if (f=='n')
cout<<"\nMatch not found ";
else
readfile();
f3.close();
break;}}