-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.seekp_and_seekg_using_pos.cpp
159 lines (132 loc) · 2.38 KB
/
8.seekp_and_seekg_using_pos.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
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
#include<iostream>
#include<conio.h>
#include<string.h>
#include<fstream>
using namespace std;
class directory
{
private:
char tno[10];
char nm[30];
char fname[20]; //according to user directory
public:
directory()
{
cout<<"Enter the file name : ";
cin>>fname;
}
void createfile()
{
ofstream fw; //object of class ofstream
fw.open(fname,ios::out|ios::app); //
if(!fw) //fw==NULL **%%
{
cout<<"Error opening the file"<<endl;
return;
}
// cout<<"successful";
while(1)
{
fflush(stdin);
cout<<"\nEnter the name : ";
gets(nm);
if(strcmp(nm,"&")==0)
break;
cout<<"\nEnter the telephone no. : ";
cin>>tno;
//cout<<"my name is"<<nm;
fw<<nm<<" "<<tno<<endl;
}
fw.close();
}
void display()
{
ifstream fi;
fi.open(fname,ios::in);
if(!fi)
{
cout<<"Error opening the file"<<endl;
return;
}
fi>>nm>>tno;
while(!fi.eof())
{
cout<<nm<<" "<<tno<<"\n";
fi>>nm>>tno;
}
fi.close();
}
void update_tell_no()
{
int pos;
char search_name[20];
char new_tellno[20];
fstream f;
ofstream tf;
tf.open("tmp.txt",ios::out);
f.open(fname,ios::in);
if(!f)
{
cout<<"Error opening the file"<<endl;
return;
}
cout<<"Enter name to update tell no : ";
cin>>search_name;
// pos=f.tellg();
f>>nm>>tno;
while(!f.eof())
{
// cout<<pos<<endl;
if(strcmp(search_name,nm)==0)
{
cout<<"enter new telephone no : ";
cin>>new_tellno;
// f.seekp(pos,ios::beg);
tf<<nm<<" "<<new_tellno<<endl;
}
else
{
tf<<nm<<" "<<tno<<endl;
}
// pos=f.tellg();
f>>nm>>tno;
}
cout<<"close";
f.close();
tf.close();
remove(fname);
rename("tmp.txt",fname);
}
};
main()
{
directory d1;
int op=-1;
while(op!=0)
{
cout<<"\n1.CREATE DIRECTORY";
cout<<"\n2.SEARCH BY NAME";
cout<<"\n3.SEARCH BY TELEPHONE NO";
cout<<"\n4.UPDATE TELEPHONE NO";
cout<<"\n5.EXIT";
cout<<"\n\nenter choice : ";
cin>>op;
switch(op)
{
case 1:
d1.createfile();
break;
case 2:
d1.display();
break;
case 3:
break;
case 4:
d1.update_tell_no();
break;
case 5:
op=0;
break;
}
}
}