Help me out!!!!

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.

CODE:

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
#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;}}
Last edited on
Could you please put it in the code tags? It's unreadable now.
sorry but i am a newbie and dont know what are code tags. Please give an example.
[code]Your code goes here[/code]

^ put those tags around your code ^
Topic archived. No new replies allowed.