-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_FILE.cpp
115 lines (103 loc) · 2.1 KB
/
1_FILE.cpp
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
#include<iostream>
#include<conio.h>
#include<string.h>
#include<fstream>
using namespace std;
class info
{
private:
char name[20];
char roll_no[10];
char email_id[30];
public:
friend class data;
};
class data
{
private:
char file_name[10];
info ob;//only one obj imp
public:
//-------------------------------------------------------------------
data()
{
cout<<"Enter file name : ";
cin>>file_name;
}
//-------------------------------------------------------------------
void createfile()
{
ofstream f;
f.open(file_name,ios::out | ios::app | ios::binary);
//why | xor
//if binary not specified it will be a text file
if(!f)
{
cout<<"\n***error opening***";
return;
}
cout<<"\n***enter name & to end file***\n\n";
while(1)
{
cout<<"NAME : ";
cin>>ob.name;
if(strcmp(ob.name,"&")==0)
break;
cout<<"ROLL NO. : ";
cin>>ob.roll_no;
cout<<"EMAIL ID : ";
cin>>ob.email_id;
f<<ob.name<<ob.roll_no<<ob.email_id;
//f.write((char*)&ob,sizeof(ob));//can use f<< instead of it
cout<<"\n";
}
f.close();
}
//-------------------------------------------------------------------
void display()
{
ifstream f;
f.open(file_name,ios::in | ios::binary);
if(!f)
{
cout<<"\n***error opening***";
return;
}
// f.seekg(5);
// f.read((char*)&ob,sizeof(ob));
f>>ob.name>>ob.roll_no>>ob.email_id;
while(!f.eof())//!f.eof()
{
cout<<"NAME : "<<ob.name<<" ROLL NO : "<<ob.roll_no<<" EMAIL ID : "<<ob.email_id<<endl;
// f.read((char*)&ob,sizeof(ob));
f>>ob.name>>ob.roll_no>>ob.email_id;
}
f.close();
}
//-------------------------------------------------------------------
};
main()
{
data d1;
int op=-1;
while(op!=0)
{
cout<<"\n1.CREATE FILE";
cout<<"\n2.DISPLAY";
cout<<"\n3.EXIT";
cout<<"\n\nenter choice : ";
cin>>op;
switch(op)
{
case 1:
d1.createfile();
break;
case 2:
d1.display();
break;
case 3:
op=0;
break;
}
}
}