Вы находитесь на странице: 1из 17

FILE Handling in C++ program

Files are used to store data in a relatively permanent form, on floppy disk, hard disk, tape or other form
of secondary storage. Files can hold huge amounts of data if need be. Ordinary variables (even records
and arrays) are kept in main memory which is temporary and rather limited in size. The following is a
comparison of the two types of storage . !ain !emory " #econdary !emory
$%% provides the following classes to perform output and input of characters to&from files'
( ofstream: #tream class to write on files
( ifstream: #tream class to read from files
( fstream: #tream class to both read and write from&to files.
Data Type Description
ofstream This data type represents the output file stream and is used to create files and to
write information to files.
ifstream This data type represents the input file stream and is used to read information from
files.
fstream This data type represents the file stream generally, and has the capabilities of both
ofstream and ifstream which means it can create files, write information to files, and
read information from files.
To perform file processing in $%%, header files )fstream.h* must be included in your $%% source file.
These classes are derived directly or indirectly from the classes istream, and ostream. +e have already
used ob,ects whose types were these classes' cin is an ob,ect of class istream and cout is an ob,ect of
class ostream. Therfore, we have already been using classes that are related to our file streams. -nd in
fact, we can use our file streams the same way we are already used to use cin and cout, with the only
difference that we have to associate these streams with physical files.
Open a file
The first operation generally performed on an ob,ect of one of these classes is to associate it to a real
file. This procedure is known as to open a file. -n open file is represented within a program by a
stream ob,ect (an instantiation of one of these classes, in the previous e.ample this was myfile) and
any input or output operation performed on this stream ob,ect will be applied to the physical file
associated to it.
There are two methods of opening a file
$onstructor
Open #tatement
/n order to open a file with a stream ob,ect we use its member function open()' open (filename, mode)0
+here filename is a null1terminated character se2uence of type const char 3 (the same type that string
literals have) representing the name of the file to be opened, and mode is an optional parameter with a
combination of the following flags'
ios''in Open for input operations or read mode
ios''out Open for output operations or write mode
ios''binary Open in binary mode.
ios''ate #et the initial position at the end of the file.
/f this flag is not set to any value, the initial position is the beginning of the file.
ios''app -ll output operations are performed at the end of the file, appending the content to the current
content of the file. This flag can only be used in streams open for output1only operations.
ios''trunc /f the file opened for output operations already e.isted before, its previous content is deleted
and replaced by the new one.
ios''nocreate Open for output operation but will not create a file only replaces the previous contents of
the file.
ios''no replace Open for output operation but will not replace the previous contents only create a new
file.
-ll these flags can be combined using the bitwise operator O4 (5). For e.ample, if we want to open the
file e.ample.bin in binary mode to add data we could do it by the following call to member function
open()'
ofstream myfile0
myfile.open (6e.ample.bin6, ios''out 5 ios''app 5 ios''binary)0
7ach one of the open() member functions of the classes ofstream, ifstream and fstream has a default
mode that is used if the file is opened without a second argument'
#ince the first task that is performed on a file stream ob,ect is generally to open a file, these three
classes include a constructor that automatically calls the open() member function and has the e.act
same parameters as this member. Therefore, we could also have declared the previous myfile ob,ect
and conducted the same opening operation in our previous e.ample by writing'
ofstream myfile (6e.ample.bin6, ios''out 5 ios''app 5 ios''binary)0
$ombining ob,ect construction and stream opening in a single statement. 8oth forms to open a file are
valid and e2uivalent. To check if a file stream was successful opening a file, you can do it by calling to
member is9open() with no arguments. This member function returns a bool value of true in the case
that indeed the stream ob,ect is associated with an open file, or false otherwise'
if (myfile.is9open()) : &3 ok, proceed with output 3& ;
class default mode parameter
ofstream ios''out
ifstream ios''in
fstream ios''in 5 ios''out
For ifstream and ofstream classes, ios''in and ios''out are automatically and respectivelly assumed,
even if a mode that does not include them is passed as second argument to the open() member
function. The default value is only applied if the function is called without specifying any value for the
mode parameter. /f the function is called with any value in that parameter the default mode is
overridden, not combined. File streams opened in binary mode perform input and output
operations independently of any format considerations. on!binary files are "no#n as text files$
and some translations may occur due to formatting of some special c%aracters &li"e ne#line and
carriage return c%aracters'.
Closing a file
+hen we are finished with our input and output operations on a file we shall close it so that its
resources become available again. /n order to do that we have to call the stream<s member function
close(). This member function takes no parameters, and what it does is to flush the associated buffers
and close the file'
myfile.close()0
Once this member function is called, the stream ob,ect can be used to open another file, and the file is
available again to be opened by other processes.
/n case that an ob,ect is destructed while still associated with an open file, the destructor automatically
calls the member function close().
Te(t files
Te.t file streams are those where we do not include the ios''binary flag in their opening mode. These
files are designed to store te.t and thus all values that we input or output from&to them can suffer some
formatting
transformations, which do not necessarily correspond to their literal binary value.
Data output operations on te(t files are performed in t%e same #ay #e operated #it% cout:
&& writing on a te.t file
=include )iostream*
=include )fstream*
void main () :
ofstream myfile (6e.ample.t.t6)0
if (myfile.is9open())
:
myfile )) 6This is a line.>n60
myfile )) 6This is another line.>n60
myfile.close()0
;
else cout )) 6?nable to open file60
;
@filee.ample.t.tA'
This is a line.
This is another line.
Data input from a file can also be performed in t%e same #ay t%at #e did #it% cin:
&& reading a te.t file
=include )iostream*
This is a line.
=include )fstream*
=include )string*
void main () :
char line@BCA0
ifstream myfile (6e.ample.t.t6)0
if (myfile.is9open())
:
while (myfile.getline(line,BC)))
:
cout )) line )) endl0
;
myfile.close()0
;
else cout )) 6?nable to open file60
;
Output'
This is a line.
This is another line.
This last e.ample reads a te.t file and prints out its content on the screen. Dotice how we have used a
new member function, called eof() that returns true in the case that the end of the file has been reached.
+e have
created a while loop that finishes when indeed myfile.eof() becomes true (i.e., the end of the file has
been reached).
C%ec"ing state flags
eof&'
4eturns true if a file open for reading has reached the end.
Te(t Files &)rogram
=include )fstream.h*
=include )conio.h* //Text File Handling Program
=include )ctype.h*
=include )stdio.h*
*oid $reate() //Function to create new Text File
:
c%ar $h0
cout))6Eelete the e.isting data(if any),-re Fou #ure (F&D)G60cin**$h0
if (toupper($h)HH<F<)
:
fstream Fil0 c%ar Iin@BCA,J0
Fil.open(6#T?E.TKT6,ios''out)0
do
:
cout))L7nter Te.tL0
gets(Iin)0
Fil))Iin))endl0
cout))L!ore(F&D)GL0cin**J0
;
#%ile (JHHMFM)0
Fil.close()0
;
;
*oid -dd-t7nd() //Function to Add new lines to an existing Text File
:
fstream Fil0 c%ar Iin@BCA,J0
Fil.open(6#T?E.TKT6,ios''app)0
do
:
cout))L7nter Te.tL0gets(Iin)0
Fil))Iin))endl0
cout))L!ore(F&D)GL0cin**J0
;
#%ile (JHHMFM)0
Fil.close()0
;
*oid Eisplay() //Function to Display the content
: //of a Text File
fstream Fil0 c%ar Iin@BCA0
Fil.open(6#T?E.TKT6,ios''in)0
#%ile (Fil.getline(Iin,BC)) cout))Iin))endl0
Fil.close()0
;int $ount() //Function to Count the number
: //of lines in a Text File
fstream Fil0
Fil.open(6#T?E.TKT6,ios''in)0
c%ar Iin@BCA0int $ntHC0
#%ile (Fil.getline(Iin,BC)) $nt%%0
Fil.close()0
return $nt0
;
*oid main()
:
clrscr()0
c%ar $hoice0
do
:
cout))6The no.of Iines in file'6))$ount()))6 Ob,ects ...6))endl0
cout))6$'$reate&-'-ddatend&E'Eisplay&J'Juit6))endl0cin**$hoice0
s#itc%($hoice)
:
case <$<'$reate()0brea"0
case <-<'-dd-t7nd()0brea"0
case <E<'Eisplay()0brea"0
;
;
#%ile ($hoiceNH<J<)0
;
=include)fstream.h* &&33 8/D-4F F/I7 O-DEI/DP 33
=include)stdio.h*
=include)conio.h*
struct stud
: int rno0
char name@CA0
int marks0
;0
void create() && F?D$T/OD TO $47-T7 D7+ F/I7
:
stud rec0
fstream fl0
fl.open(6test.dat6, ios''out5ios''binary)0
for (int i H 0i)HQ0i%%)
:
cout ))6r.no. ' 60
cin**rec.rno0
cout ))6name ' 6 0
gets(rec.name)0
cout ))6marks ' 6 0
cin**rec.marks0
fl.write((char 3 )Rrec,sizeof(rec))0
;
fl.close()0
;
void addatend() && F?D$T/OD TO-EE D7+ 47$O4E#
: stud rec0 && -T 7DE OF TO7 F/I7
fstream fl0
char J0
fl.open(6test.dat6, ios''app 5 ios''binary)0
do
:
cout ))6>n7nter The 4ecord ' >n>n60
cout ))6r.no. ' 60
cin**rec.rno0
cout ))6name ' 6 0
gets(rec.name)0
cout ))6marks ' 6 0
cin**rec.marks0
fl.write((char 3 )Rrec,sizeof(rec))0
cout ))6>n!ore )F&D*'60cin**J0
;
while(JHH<F<)0
fl.close()0
;
void display() && F?D$T/OD TO E/#SI-F -II 47$O4E#
:
stud rec0
fstream fl0
fl.open(6test.dat6, ios''in)0
fl.read((char 3 )Rrec,sizeof(rec))0
while (fl) &&or while (Nfl.eof())
:
cout ))6>nr.no. ' 6))rec.rno0
cout ))6>nname ' 6 ))rec.name0
cout ))6>nmarks ' 6))rec.marks0
fl.read((char 3 )Rrec,sizeof(rec))0
;
fl.close()0
getch()0
;
void search() && F?D$T/OD TO #7-4$O 47$O4E
: && FO4 P/T7D 4OII DO.
stud rec0
fstream fl0
clrscr()0
int r,foundHC0
cout ))6Pive 4oll Do To 8e #earched ' 60
cin **r0
fl.open(6test.dat6, ios''in)0
fl.read((char 3 )Rrec,sizeof(rec))0
while (fl)&& while (Nfl.eof())
: if (rec.rnoHHr)
:
cout ))6>nr.no. ' 6))rec.rno0
cout ))6>nname ' 6 ))rec.name0
cout ))6>nmarks ' 6))rec.marks0
foundH0
;
fl.read((char 3 )Rrec,sizeof(rec))0
;
if (foundHHC) cout ))6>n/nvalid 4oll Do.60
fl.close()0
getch()0
;
void main()
:
char choice0
do:
clrscr()0
cout))6>n>n>t$'$reate>n>t-'-dd at end>n>tE'Eisplay60
cout))6>n>t#'#earch>n>tJ'Juit>n>t60cin**choice0
switch(choice)
:
case <$< ' create()0break0
case <-< ' addatend()0break0
case <E< ' display()0break0
case <#< ' search()0break0
;
;
while(choiceNH<J<)0
;
+include,fstream.%-
+include,conio.%-
+include,stdio.%-
+include,process.%-
*oid count&'
.
c%ar fname/012$c%3
cout,,45n Enter t%e file name :43
gets&fname'3
ifstream fin&fname'3
$ount of characters
and lines
c%ar str/6123
int n71$lines713
#%ile&fin.get&c%''
.
n++3
if&c%7785n8'
lines++3
9
cout,,45n Total no. of c%aracters:43
cout,,n3
cout,,:5n Total no of lines ;,,lines3
fin.close&'3
9
*oid count<line&'
.
cout,,45n Enter t%e file name :43
gets&fname'3
ifstream fin&fname'3
c%ar str/6123
#%ile&fin.getline&str$61''
line++3
cout,,45n o. of lines:43
cout,,line3
fin.close&'3
9
*oid count<alp%a&'
.
cout,,45n Enter t%e file name :43
gets&fname'3
ifstream fin&fname'3
#%ile&fin.get&c%''
.
if&c%-7=>??c%,7@1'
upper++3
else
if&c%-7A1A??c%,7A@A'
digit++3
else
if&c%77A A'
#ord++3
s#itc%&c%'
.
case AaA:
case ABA:
case AeA:
case AEA:
case AiA:
case AIA:
case AoA:
4ead $haracter by
$haracter till eof
Fou can use isupper() etc
functions but donMt forget
to include ctype.h
-nother method of
counting lines
-nother method is to read word by word
using fin.getline(str,BC,M U) method
case AOA:
case AuA:
case ACA:
*o#el++3
brea"3
9
9
cout,,45n umber of upper case c%aracters :43
cout,,upper3
cout,,45n umber of *o#els :43
cout,,*o#el3
cout,,45n umber of digits :43
cout,,digit3
cout,,45n umber of #ords :43
cout,,#ord3
fin.close&'3
9
*oid modify<file&'
.
c%ar fname/6123
clrscr&'3
cout,,45n5aETED THE EOCDCE FILE BFE43
gets&fname'3
fstream fin&fname$ios::inGios::out'3
c%ar c%H$c%0$c%3
int c%oice3
if&Ifin'
cout,,45nFILE DOEEAT EJIET43
cout,,45nETED THE CHBDBCTED TO KE FOCD43
cin--c%H3
cout,,45nETED THE CHBDBDCTED TO DE)LBCE43
cin--c%03
fin.see"p&1'3
#%ile&fin.get&c%''
.
if&c%77c%H'
.
fin.see"p&!H$ios::cur'3
fin,,c%03
9
9
fin.close&'3
getc%&'3
9
*oid copy<file&'
.
c%ar fnameH/612$fname0/6123
clrscr&'3
cout,,45n5aETED THE EOCDCE FILE BFE43
gets&fname'3
cout,,:5n Enter Target file BFE:3
gets&fname0'3
fstream fin&fname$ios::in'3
fstream fout&fname$ios::out'3
c%ar c%3
if&Ifin'
cout,,45nFILE DOEEAT EJIET43
#%ile&fin.get&c%''
.
fout,,c%3
9
fin.close&'3
fout.close&'3
getc%&'3
9
File Sosition Sointers'
8oth istream and ostream provide member functions for repositioning the file1position pointer. These
member functions are see"g (6seek get6) for istream and see"p (6seek put6) for ostream.
The argument to seekg and seekp normally is a long integer. - second argument can be specified to
indicate the seek direction. The seek direction can be ios::beg (the default) for positioning relative to
the beginning of a stream, ios::cur for positioning relative to the current position in a stream
or ios::end for positioning relative to the end of a stream.
The file1position pointer is an integer value that specifies the location in the file as a number of bytes
from the file<s starting location. #ome e.amples of positioning the 6get6 file1position pointer are'
&& position to the nth byte of fileOb,ect (assumes ios''beg)
fileOb,ect.seekg( n )0
&& position n bytes forward in fileOb,ect
fileOb,ect.seekg( n, ios''cur )0
&& position n bytes back from end of fileOb,ect
fileOb,ect.seekg( n, ios''end )0
&& position at end of fileOb,ect
fileOb,ect.seekg( C, ios''end )0
(
BEEILFET :
M Bssuming t%e class NEHICLE gi*en belo#$ #rite function in C++ to perform follo#ing:
8efor writing data can be converted like
if (isupper(ch)
chHtolower(ch)0
-fter closing remove,
rename functions can be
used for deletion and
modification
H. Orite t%e obPects of NEHICLE to a binary file.
0. Dead t%e obPects of NEHICLE from binary file and display t%em on screen.
class NEHICLE
. int #%eels$ passenger 3
public :
*oid inputdata& ' . cin -- #%eels 3 gets & passenger' 3 9
*oid outputdata & ' . cout,,#%eels,, ; ; ,, passenger,,endl3 9
93
QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
*oid create<file& '
. fstream fl3
NEHICLE *3 RR QQQQQ obPect of class NEHICBL created QQQQQQQQQ
C%ar ans3
int siSe 7 siSeof&*'3RR
<<<<<<<<<<<<<<<<<<<<<<<<<RRopen t%e file in OCT)CT mode assuming file
RR name is ;NEHICBL.DBT: use open& ' function
do
. *.inputdata& '3
fl.#rite&<<<<<<<<$<<<<<<<'3RR ODITE THE CLBEE OKTECT O FILE
cout ,,:5n Do you #ant to #rite anot%er record in file :,yRn- : ; 3
cin -- ans3
9
#%ile &ans778U8 GG ans778y8'3
<<<<<<<<<<<<<<<3 RR close t%e opened file
9 RR end of create<file& ' function
QQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQQ
*oid display<file& '
.
fstream fl3
NEHICLE *3
<<<<<<<<<<<<<<<<<<<<<<<<<3 RRopen t%e file in I)CT mode assuming file
RR name is ;NEHICBL.DBT: using COETDCCTOD
#%ile& <<<<<<<<<<< ' RRc%ec" end of file
. <<<<<<<<<<<<<<<<<<<<<<<<<3RR DEBD THE CLBEE OKTECT FDOF FILE
*.outputdata& '3
9
<<<<<<<<<<<<<<<3 RR close t%e opened file
9
M. Bssuming t%e class DDIV$ #rite functions in C++ to perform t%e follo#ing:
H. Orite t%e obPect of DDIV on to a binary file.
0. Dead obPects of DDIV from t%e binary file and display on t%e screen
class DDIV
.
int Wuantity3
c%ar name/H123
public:
*oid getdata&'. gets&name'3 cin--Wuantity39
*oid s%o#data&'.cout,,Wuantity,, ; ;,,name,,endl39
93
Class: K// -, K// E
T%e contents of a file can be deleted by opening a temporary file.
Logic
. Open the file in read mode
". Open the temp file in write mode
V. -ccept character to delete
Q. 4ead file
W. /f character is not matching write in temp file
X. 4epeat step Q1W till end of file
Y. $lose both the files
B. 4emove original file
Z. 4ename the temp file.
Srogram of updating te.t file
=include)fstream.h*
=include)stdio.h*
=include)conio.h*
void main()
:
fstream fin([abc.t.tL,ios''in)0
fstream fout([temp.t.tL,ios''out)0
char ch,ch0
cout))L>n enter character for deletionL0
cin**ch0
while(fin.get(ch))
:
/f (chNHch)
fout))ch0
;
fin.close()0
fout.close()0
remove([abc.t.tL)0
rename([temp.t.t,Labc.t.tL)0
;
T%e contents of a file can be modified by t#o met%ods
?sing temporary file
" +ithout using temporary file
Logic of modification using temporary file
. Open the file in read mode
". Open the temp file in write mode
V. -ccept character to find and replace
Q. 4ead file
W. /f character is not matching write in temp file and write replace character if matching
X. 4epeat step Q1W till end of file
Y. $lose both the files
B. 4emove original file
Z. 4ename the temp file.
+rite the non matching
character in temp file.
4emove the
original file
#ave temp file as
original file
retaining other
data after
removing the data
to be deleted.
Srogram of updating te.t file
=include)fstream.h*
=include)stdio.h*
=include)conio.h*
void main()
:
fstream fin([abc.t.tL,ios''in)0
fstream fout([temp.t.tL,ios''out)0
char ch,ch,ch"0
cout))L>n enter character to findL0
cin**ch0
cout))L>n 7nter character to replaceL0
cin**ch"0
while(fin.get(ch))
:
/f (chNHch)
fout))ch0
else
fout))ch"0
;
fin.close()0
fout.close()0
remove([abc.t.tL)0
rename([temp.t.t,Labc.t.tL)0
;
Once a character is read&write the file pointer moves to the ne.t byte so inorder to rewrite
seekp and seekg commands are used to move the file pointer.
#eekp is put pointer for writing and seekg is a get pointer for reading.
Fin.seekp(V)0 Slace the file pointer on V
rd
byte
Fin.seekg(1",ios''cur)0 Slace file pointer " bytes back from current position
Fore e(amples #ill be ta"en to e(plain t%e concept.
Logic of modification #it%our using temp file.
. Open the file in rewrite mode
". -ccept character to find and replace
V. 4ead file
Q. /f character is matching move file pointer back by byte and write in file
W. 4epeat step V1Q till end of file
X. $lose the files
Srogram of updating te.t file
=include)fstream.h*
=include)stdio.h*
=include)conio.h*
void main()
:
fstream fin([abc.t.tL,ios''in5ios''out)0
char ch,ch,ch"0
+rite the original character
of non matching character
and replace character for
matching in temp file.
4emove the
original file
#ave temp file as
original file
retaining data
after modification.
/f character is matching
move file pointer back
by byte and rewrite in
the file.
cout))L>n enter character to findL0
cin**ch0
cout))L>n enter character to replaceL0
cin**ch"0
while(fin.get(ch))
:
/f (chHHch)
:
Fin.seekp(1,ios''cur)0
fin))ch"0
;
;
fin.close()0
;
EeWuential file organiSation : In seWuential files updation is not possible in a single file$ so t#o
or more files are reWuired for t%e same. Kinary Data File is a file in #%ic% #e read or #rite a
#%ole record. Oe first need to design t%e layout or structure to #rite and t%en #rite in file.
struct student
:
int roll0
char name@"CA0
int marks0
;0
void write9file( )
:
fstream outfl0
outfl.open([stud.datL , ios '' out 5 ios''binary)0&& open file in binary and output mode
student s0
char rep0
do
:
cout))L7nter dataL0
cin ** s.roll0
gets(s.name)0
cin ** s.marks0
outfl.write((char3)Rs , sizeof(s))0
cout))L>n +ant to add more0
cin**rep0
;while(repHHMyM)0
outfl.close()0
;
void read9file( )
:
fstream infl0
infl.open([stud.datL , ios '' int 5 ios''binary)0&& open file in binary and input mode
student s0
while(infl.read((char 3)Rs,sizeof(s)))
:
cout))LEata'L))s.roll)))L>tL))s.name))s.marks))endl0
;
infl.close()0
;
=include)fstream.h* &&33 8/D-4F F/I7 O-DEI/DP 33
=include)stdio.h*
=include)conio.h*
class stud
: int rno0
char name@CA0
int marks0
public'
void getdata()
:
cout ))6rollno. ' 60
cin**rno0
cout ))6name ' 6 0
gets(name)0
cout ))6marks ' 6 0
cin**marks0
;
void dispdata()
:
cout ))6>nr.no. ' 6)) rno0
cout ))6>nname ' 6 ))name0
cout ))6>nmarks ' 6))marks
;
int return9rno():return rno0;
;0
void create() && F?D$T/OD TO $47-T7 D7+ F/I7
:
stud rec0
fstream fl0
fl.open(6test.dat6, ios''out5ios''binary)0
for (int i H 0i)HQ0i%%)
:
rec.getdata()0
fl.write((char 3 )Rrec,sizeof(rec))0
;
fl.close()0
;
void addatend() && F?D$T/OD TO-EE D7+ 47$O4E#
: stud rec0 && -T 7DE OF TO7 F/I7
fstream fl0
char J0
fl.open(6test.dat6, ios''app 5 ios''binary)0
do
:
rec.getdata()
$lass for writing in the file.
Open file to for
writing
Open file
for adding
the data.
+rite the
record in the
file.
void modify_rec() //using seekp
{
STUD s;
int flag=0;
fstream
file(fname,ios::inary,ios::in!
ios::inary);
int r;
co"t##$%n &nter rollno for
modification$;
cin''r;
if(file==(U)))
{
co"t##*%n +ile does not e,ist*;
ret"rn;
-
./ile(file0read((c/ar1)2s,si3eof(s)))
{
if(r==s0ret"rn_rno())
{
s0getdata();
flag=4;
file0see56(7
41si3eof(s),ios::c"r);
file0.rite((c/ar1)2s,si3eof(STUD));
-
if(flag==0)co"t##*%n8oll no
does not e,ist*;
else
co"t##*%n 8ecord modified$;
file0close();
;
fl.write((char 3 )Rrec,sizeof(rec))0
cout ))6>n!ore )F&D*'60cin**J0
;
while(JHH<F<)0
fl.close()0
;
void display() && F?D$T/OD TO E/#SI-F -II 47$O4E#
:
stud rec0
fstream fl0
fl.open(6test.dat6, ios''in)0
fl.read((char 3 )Rrec,sizeof(rec))0
while (fl) &&or while (Nfl.eof())
:
rec.dispdata()0
fl.read((char 3 )Rrec,sizeof(rec))0
;
fl.close()0
getch()0
;
void search() && F?D$T/OD TO #7-4$O 47$O4E
: && FO4 P/T7D 4OII DO.
stud rec0
fstream fl0
clrscr()0
int r,foundHC0
cout ))6Pive 4oll Do To 8e #earched ' 60
cin **r0
fl.open(6test.dat6, ios''in)0
while(fl.read((char 3 )Rrec,sizeof(rec)))
: if (rec.return9rnoHHr)
:
rec.dispdata()0
foundH0
;
;
if (foundHHC) cout ))6>n/nvalid 4oll Do.60
fl.close()0
getch()0
;
void Eelete9rec()
:
stud s0
int flagHC0
ifstream ifile(fname,ios''binary)0
if(ifileHHD?II)
:
cout))6>n File does not e.ist60
return0
4ead file to read
a records from
the file.
#earch the
rollno to
display
void modify_rec() //using seekp
{
STUD s;
int flag=0;
fstream
file(fname,ios::inary,ios::in!
ios::inary);
int r;
co"t##$%n &nter rollno for
modification$;
cin''r;
if(file==(U)))
{
co"t##*%n +ile does not e,ist*;
ret"rn;
-
./ile(file0read((c/ar1)2s,si3eof(s)))
{
if(r==s0ret"rn_rno())
{
s0getdata();
flag=4;
file0see56(7
41si3eof(s),ios::c"r);
file0.rite((c/ar1)2s,si3eof(STUD));
-
if(flag==0)co"t##*%n8oll no
does not e,ist*;
else
co"t##*%n 8ecord modified$;
file0close();
;
;
int r0
cout))L>n 7nter rollno for deltionL0
cin**r0
ofstream ofile(6temp.dat6,ios''binary)0
while(ifile.read((char3)Rs,sizeof(#T?E)))
:
if(rNHs.return9rno())
ofile.write((char3)Rs,sizeof(#T?E))0
else
flagH0
;
if(flagHHC)cout))6>n4oll no does not e.ist60
else
cout))6>n 4ecord deleted60
ifile.close()0
ofile.close()0
remove(fname)0
rename(6temp.dat6,fname)0
;
void modify9rec()
:
#T?E s0
int flagHC0
ifstream ifile(fname,ios''binary)0
int r0
cout))L>n 7nter rollno for modificationL0
cin**r0
if(ifileHHD?II)
:
cout))6>n File does not e.ist60
return0
;
ofstream ofile(6temp.dat6,ios''binary)0
while(ifile.read((char3)Rs,sizeof(#T?E)))
:
if(rHHs.return9rno())
:
s.getdata()0
flagH0 ;
ofile.write((char3)Rs,sizeof(#T?E))0 ; ;
if(flagHHC)
cout))6>n4oll no does not e.ist60
else
cout))6>n 4ecord modifiedL0
ifile.close()0 ofile.close()0
remove(fname)0
rename(6temp.dat6,fname)0 ;
void modify_rec() //using seekp
{
STUD s;
int flag=0;
fstream
file(fname,ios::inary,ios::in!
ios::inary);
int r;
co"t##$%n &nter rollno for
modification$;
cin''r;
if(file==(U)))
{
co"t##*%n +ile does not e,ist*;
ret"rn;
-
./ile(file0read((c/ar1)2s,si3eof(s)))
{
if(r==s0ret"rn_rno())
{
s0getdata();
flag=4;
file0see56(7
41si3eof(s),ios::c"r);
file0.rite((c/ar1)2s,si3eof(STUD));
-
if(flag==0)co"t##*%n8oll no
does not e,ist*;
else
co"t##*%n 8ecord modified$;
file0close();
;

Вам также может понравиться