Working With Fi
Working With Fi
kingwi
thFi
l
e
Fi
l
eHandl
i
ngusi
ngFi
l
eSt
reamsi
nC++
Fi
l
er epresentsstoragemedi um forstor
ingdat
aorinformati
on.Str
eamsr ef
ert
osequenceof
by
tes.InFileswest oredat ai
.e.t
extorbinar
ydatapermanentlyandusethesedat
atoreador
wri
teinthef orm ofinputoutputoperati
onsbytr
ansferr
ingbytesofdat
a.Soweusetheterm
Fi
l
eSt reams/ Fi
lehandling.Weuset heheaderfi
le<f
stream>
of
str
eam:
Itr
epr
esent
sout
putSt
ream andt
hisi
susedf
orwr
it
ingi
nfi
l
es.
i
fst
ream:
Itr
epr
esent
sinputSt
ream andt
hisi
susedf
orr
eadi
ngf
rom f
il
es.
f
str
eam:
Itr
epr
esent
sbot
hout
putSt
ream andi
nputSt
ream.Soi
tcanr
eadf
rom f
il
esandwr
it
e
t
ofi
l
es.
Oper
ati
onsi
nFi
l
eHandl
i
ng:
Cr
eat
ingaf
il
e:open(
)
Readi
ngdat
a:r
ead(
)
Wr
it
ingnewdat
a:wr
it
e()
Cl
osi
ngaf
il
e:cl
ose(
)
Cr
eat
ing/
Openi
ngaFi
l
e
Wecr
eat
e/openaf
il
ebyspeci
fyi
ngnewpat
hoft
hef
il
eandmodeofoper
ati
on.Oper
ati
onscan
ber
eadi
ng,
wri
ti
ng,
appendi
ngandt
runcat
ing.
Sy
ntaxf
orf
il
ecr
eat
ion : Fi
l
ePoi
nter
.open(
"Pat
h",
i
os:
:
mode)
;
Exampl
eoff
il
eopenedf
orwr
it
ing : st
.open(
"E:
\st
udy
toni
ght
.t
xt"
,i
os:
:
out
);
Exampl
eoff
il
eopenedf
orr
eadi
ng : st
.open(
"E:
\st
udy
toni
ght
.t
xt"
,i
os:
:
in)
;
Exampl
eoff
il
eopenedf
orappendi
ng : st
.open(
"E:
\st
udy
toni
ght
.t
xt"
,i
os:
:
app)
;
Exampl
eoff
il
eopenedf
ort
runcat
ing : st
.open(
"E:
\st
udy
toni
ght
.t
xt"
,i
os:
:
trunc)
;
Cr
eat
enewFi
l
e
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
f
str
eam f
il
e;/
/obj
ectoff
str
eam cl
ass
/
/openi
ngf
il
e"sampl
e.t
xt"i
nout
(wr
it
e)mode
f
il
e.open(
"sampl
e.t
xt"
,i
os:
:
out
);
i
f(
!f
il
e)
cout
<<"
Err
ori
ncr
eat
ingf
il
e!!
!"
;
r
etur
n0;
cout
<<"
Fil
ecr
eat
edsuccessf
ull
y.
";
/
/cl
osi
ngt
hef
il
e
f
il
e.cl
ose(
);
r
etur
n0;
}
Out
put
Fi
l
ecr
eat
edsuccessf
ull
y.
Wr
it
ingt
oaFi
l
e
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
f
str
eam st
;//St
ep1:
Creat
ingobj
ectoff
str
eam cl
ass
st
.open(
"st
udy
toni
ght
.t
xt"
,i
os:
:
out
);/
/St
ep2:
Creat
ingnewf
il
e
i
f(
!st
)//St
ep3:
Checki
ngwhet
herf
il
eexi
st
cout
<<"
Fil
ecr
eat
ionf
ail
ed"
;
el
se
cout
<<"
Newf
il
ecr
eat
ed"
;
st
<<"
Hel
l
o";/
/St
ep4:
Wri
ti
ngt
ofi
l
e
st
.cl
ose(
);/
/St
ep5:
Closi
ngf
il
e
get
ch(
);
r
etur
n0;
}
Herewearesendi
ngoutputt
oafil
e.So,weuseios::
out.Asgiv
enintheprogr
am,
i
nformat
iont
ypedinsi
dethequot
esafter"
Fil
ePoi
nter<<"wil
lbepassedtooutputf
il
e.
Readi
ngf
rom aFi
l
e
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
f
str
eam st
;//st
ep1:
Creat
ingobj
ectoff
str
eam cl
ass
st
.open(
"st
udy
toni
ght
.t
xt"
,i
os:
:
in)
;//St
ep2:
Creat
ingnewf
il
e
i
f(
!st
)//St
ep3:
Checki
ngwhet
herf
il
eexi
st
cout
<<"
Nosuchf
il
e";
el
se
charch;
whi
l
e(!
st.
eof
())
st>>ch;/
/St
ep4:
Readi
ngf
rom f
il
e
cout<<ch;/
/MessageReadf
rom f
il
e
st
.cl
ose(
);/
/St
ep5:
Closi
ngf
il
e
}
get
ch(
);
r
etur
n0;
Out
put
Hel
l
o
ReadandWr
it
eExampl
e
#i
ncl
ude<f
str
eam.
h>
#i
ncl
ude<i
ost
ream.
h>
i
ntmai
n()
chardat
a[100]
;
/
/openaf
il
einwr
it
emode.
of
str
eam out
fi
le;
out
fi
le.
open(
"af
il
e.dat
");
cout<<"
Wri
ti
ngt
othef
il
e"<<endl
;
cout<<"
Ent
ery
ourname:
";
ci
n.get
li
ne(
dat
a,100)
;
/
/wr
it
einput
teddat
aint
othef
il
e.
out
fi
le<<dat
a<<endl
;
cout<<"
Ent
ery
ourage:
";
ci
n>>dat
a;
ci
n.i
gnor
e()
;
/
/agai
nwr
it
einput
teddat
aint
othef
il
e.
out
fi
le<<dat
a<<endl
;
/
/cl
oset
heopenedf
il
e.
out
fi
le.
close(
);
/
/openaf
il
einr
eadmode.
i
fst
ream i
nfi
l
e;
i
nfi
l
e.open(
"af
il
e.dat
");
cout<<"
Readi
ngf
rom t
hef
il
e"<<endl
;
i
nfi
l
e>>dat
a;
/
/wr
it
ethedat
aatt
hescr
een.
cout<<dat
a<<endl
;
/
/agai
nreadt
hedat
afr
om t
hef
il
eanddi
spl
ayi
t.
i
nfi
l
e>>dat
a;
cout<<dat
a<<endl
;
/
/cl
oset
heopenedf
il
e.
i
nfi
l
e.cl
ose(
);
r
etur
n0;
}
out
put−
Wr
it
ingt
othef
il
e
Ent
ery
ourname:
Zar
a
Ent
ery
ourage:
9
Readi
ngf
rom t
hef
il
e
Zar
a
9
Cl
oseaFi
l
e
I
tisdonebyFi
l
ePoi
nter
.cl
ose(
).
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
f
str
eam st
;//St
ep1:
Creat
ingobj
ectoff
str
eam cl
ass
st
.open(
"st
udy
toni
ght
.t
xt"
,i
os:
:
out
);/
/St
ep2:
Creat
ingnewf
il
e
st
.cl
ose(
);/
/St
ep4:
Closi
ngf
il
e
get
ch(
);
r
etur
n0;
Speci
aloper
ati
onsi
naFi
l
e
Ther
ear
efewi
mpor
tantf
unct
ionst
obeusedwi
thf
il
est
reamsl
i
ke:
t
ell
p()-I
ttel
l
sthecur
rentposi
ti
onoft
heputpoi
nter
.
Sy
ntax:
fil
epoi
nter
.t
ell
p()
t
ell
g()-I
ttel
l
sthecur
rentposi
ti
onoft
hegetpoi
nter
.
Sy
ntax:
fil
epoi
nter
.t
ell
g()
seekp(
)-I
tmov
est
heputpoi
ntert
oment
ionedl
ocat
ion.
Sy
ntax:
fil
epoi
nter
.seekp(
noofby
tes,
ref
erencemode)
seekg(
)-I
tmov
esgetpoi
nter
(i
nput
)toaspeci
fi
edl
ocat
ion.
Sy
ntax:
fil
epoi
nter
.seekg(
(noofby
tes,
ref
erencepoi
nt)
put
()-I
twr
it
esasi
ngl
echar
act
ert
ofi
l
e.
get
()-I
treadsasi
ngl
echar
act
erf
rom f
il
e.
Bel
owi
sapr
ogr
am t
oshowi
mpor
tanceoft
ell
p,t
ell
g,seekpand
seekg:
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
f
str
eam st
;//Cr
eat
ingobj
ectoff
str
eam cl
ass
st
.open(
"st
udy
toni
ght
.t
xt"
,i
os:
:
out
);/
/Cr
eat
ingnewf
il
e
i
f(
!st
)//Checki
ngwhet
herf
il
eexi
st
cout
<<"
Fil
ecr
eat
ionf
ail
ed"
;
el
se
cout
<<"
Newf
il
ecr
eat
ed"
<<endl
;
st
<<"
Hel
l
oFr
iends"
;//
Wri
ti
ngt
ofi
l
e
/
/Checki
ngt
hef
il
epoi
nterposi
ti
on
cout
<<"
Fil
ePoi
nterPosi
ti
oni
s"<<st
.t
ell
p()
<<endl
;
st
.seekp(
-1,
ios:
:
cur
);/
/Gooneposi
ti
onbackf
rom cur
rentposi
ti
on
/
/Checki
ngt
hef
il
epoi
nterposi
ti
on
cout
<<"
Aspert
ell
pFi
l
ePoi
nterPosi
ti
oni
s"<<st
.t
ell
p()
<<endl
;
st
.cl
ose(
);/
/cl
osi
ngf
il
e
st
.open(
"st
udy
toni
ght
.t
xt"
,i
os:
:
in)
;//Openi
ngf
il
einr
eadmode
i
f(
!st
)//
Checki
ngwhet
herf
il
eexi
st
cout
<<"
Nosuchf
il
e";
el
se
{
charch;
st
.seekg(
5,i
os:
:
beg)
;//Got
oposi
ti
on5f
rom begni
ng.
cout<<"
Aspert
ell
gFi
l
ePoi
nterPosi
ti
oni
s"<<st
.t
ell
g()
<<endl
;//
Checki
ngf
il
e
poi
nterposi
ti
on
cout
<<endl
;
st
.seekg(
1,i
os:
:
cur
);/
/Got
oposi
ti
on1f
rom begi
nni
ng.
cout<<"
Aspert
ell
gFi
l
ePoi
nterPosi
ti
oni
s"<<st
.t
ell
g()
<<endl
;//
Checki
ngf
il
e
poi
nterposi
ti
on
st
.cl
ose(
);/
/Cl
osi
ngf
il
e
get
ch(
);
r
etur
n0;
Newf
il
ecr
eat
ed
Fi
l
ePoi
nterPosi
ti
oni
s13
Aspert
ell
pFi
l
ePoi
nterPosi
ti
oni
s12
Aspert
ell
gFi
l
ePoi
nterPosi
ti
oni
s5
Aspert
ell
gFi
l
ePoi
nterPosi
ti
oni
s6
St
udentdat
abaseusi
ngf
il
ehandl
i
ng
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<coni
o.h>
#i
ncl
ude<st
dli
b.h>
#i
ncl
ude<st
ri
ng.
h>
#i
ncl
ude<f
str
eam.
h>
st
ructst
udent
charname[
20]
;
charr
eg[
15]
;
charcour
se[
10]
;
f
loatcgpa;
}
;
f
str
eam f
il
e;
st
udentobj
;
v
oidadd(
)
cout
<<"
Ent
erName:
";
ci
n>>obj
.name;
cout
<<"
Ent
erRegi
str
ati
onNumber
:";
ci
n>>obj
.r
eg;
cout
<<"
Ent
erCour
se:
";
ci
n>>obj
.cour
se;
cout
<<"
Ent
erCGPA:
";
ci
n>>obj
.cgpa;
f
il
e.open(
"dat
abase.
txt
",
ios:
:
app);
f
il
e.wr
it
e((
char
*)&obj
,
sizeof
(obj
))
;
f
il
e.cl
ose(
);
}
v
oidshow_
all
()
{ /
/cl
rscr
();
f
il
e.open(
"dat
abase.
txt
",
ios:
:
in)
;
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
whi
l
e(f
il
e.eof
()==0)
cout
<<"
Name:
"<<obj
.
name<<endl
;
cout
<<"
Regi
str
ati
onNumber
:"<<obj
.r
eg<<endl
;
cout
<<"
Cour
se:
"<<obj
.
cour
se<<endl
;
cout
<<"
CGPA:
"<<obj
.cgpa<<endl
<<endl
;
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
f
il
e.cl
ose(
);
get
ch(
);
v
oidsear
ch(
)
{ /
/cl
rscr
();
f
loatuser
;
cout
<<"
Ent
erCGPA:
";
ci
n>>user
;
f
il
e.open(
"dat
abase.
txt
",
ios:
:
in)
;
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
whi
l
e(f
il
e.eof
()==0)
i
f(obj
.
cgpa==user
)
cout
<<"
Name:
"<<obj
.
name<<endl
;
cout
<<"
Regi
str
ati
onNumber
:"<<obj
.r
eg<<endl
;
cout
<<"
Cour
se:
"<<obj
.cour
se<<endl
;
cout
<<"
CGPA:
"<<obj
.cgpa<<endl
<<endl
;
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
f
il
e.cl
ose(
);
get
ch(
);
v
oidedi
t(
)
{ /
/cl
rscr
();
charuser
[15]
;
cout
<<"
Ent
err
egi
str
ati
onNumber
:";
ci
n>>user
;
f
il
e.open(
"dat
abase.
txt
",
ios:
:
in|
i
os:
:
out
);
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
whi
l
e(f
il
e.eof
()==0)
i
f(st
rcmp(
obj
.r
eg,
user
)==0)
cout
<<"
Name:
"<<obj
.
name<<endl
;
cout
<<"
Regi
str
ati
onNumber
:"<<obj
.r
eg<<endl
;
cout
<<"
Cour
se:
"<<obj
.cour
se<<endl
;
cout
<<"
CGPA:
"<<obj
.cgpa<<endl
<<endl
;
cout
<<"
\nEnt
erNewcour
se:
";
ci
n>>obj
.
cour
se;
f
il
e.seekp(
fi
le.
tel
l
g()
-si
zeof
(obj
))
;
f
il
e.wr
it
e((
char
*)&obj
,
sizeof
(obj
))
;
cout
<<"
\n\
nFi
l
eUpdat
ed"
;
br
eak;
f
il
e.r
ead(
(char
*)&obj
,
sizeof
(obj
))
;
f
il
e.cl
ose(
);
get
ch(
);
v
oidmai
n()
/
/cl
rscr
();
/
/fi
l
e.open(
"dat
abase.
txt
",
ios:
:
out
);
/
/fi
l
e.cl
ose(
);
i
ntopt
ion;
whi
l
e(1)
/
/cl
rscr
();
cout
<<"
Ent
er1t
oEnt
erRecor
d\n"
;
cout
<<"
Ent
er2t
oShowAl
lRecor
d\n"
;
cout
<<"
Ent
er3t
oSear
chRecor
d\n"
;
cout
<<"
ent
er4t
oEdi
tRecor
d\n"
;
cout
<<"
Ent
er5t
oExi
t\
n";
cout
<<"
\n\
nEnt
erOpt
ion:
";
ci
n>>opt
ion;
swi
tch(
opt
ion)
case1:
add(
);
cout
<<"
\n\
nRecor
dEnt
ered\
n";
get
ch(
);
br
eak;
case2:
show_
all
()
;
br
eak;
case3:
sear
ch(
);
br
eak;
case4:
edi
t(
);
br
eak;
case5:
exi
t(
0);
get
ch(
);
}
#i
ncl
ude<i
ost
ream
C++Pr
ogr
am t
oMer
geTwoFi
l
esi
ntoaSi
ngl
efi
l
e
#i
ncl
ude<st
dio.
h>
#i
ncl
ude<st
dli
b.h>
i
ntmai
n()
i
fst
ream i
fi
les1,
ifi
l
es2;
of
str
eam i
fi
let
;
charch,
fname1[
100]
,fname2[
100]
,fname3[
100]
;
cout
<<"
Ent
erf
ir
stf
il
ename:
:";
ci
n>>f
name1;
cout
<<"
\nEnt
ersecondf
il
ename:
:";
ci
n>>f
name2;
cout
<<"
\nEnt
ert
hir
dnameoff
il
e::
";
ci
n>>f
name3;
i
fi
les1.
open(
fname1)
;
i
fi
les2.
open(
fname2)
;
i
f(
!i
fi
les1|
|!
if
il
es2)
per
ror
("\
nEr
rorMessagei
nfi
l
e1111"
);
cout
<<"
\nPr
essanykeyt
oexi
t.
..
\n"
;
exi
t(
EXI
T_FAI
LURE)
;
i
fi
let
.open(
fname3)
;
i
f(
!i
fi
let
)
per
ror
("\
nEr
rorMessage"
);
cout
<<"
\nPr
essanykeyt
oexi
t.
..
\n"
;
exi
t(
EXI
T_FAI
LURE)
;
whi
l
e(i
fi
les1.
eof
()==0)
i
fi
les1>>ch;
i
fi
let
<<ch;
whi
l
e(i
fi
les2.
eof
()==0)
i
fi
les2>>ch;
i
fi
let
<<ch;
cout
<<"
\nThet
wof
il
eswer
emer
gedi
nto"
<<f
name3<<"f
il
esuccessf
ull
y.
..
.!
!\
n";
i
fi
les1.
close(
);
i
fi
les2.
close(
);
i
fi
let
.cl
ose(
);
r
etur
n0;
}
#i
ncl
ude<i
ost
ream.
h>
#i
ncl
ude<f
str
eam.
h>
i
ntmai
n()
cout
<<"
\nTextFi
l
e1i
sopened.
..
..
..
.\
n";
i
fst
ream f
in(
"fi
l
e4.
txt
");
//openi
ngt
extf
il
e
i
ntl
i
ne=1,
wor
d=1,
si
ze;
//wi
l
lnotcountf
ir
stwor
dandl
astl
i
nesoi
nit
ial
val
uei
s1
charch;
f
in.
seekg(
0,i
os:
:
end)
;//
bri
ngf
il
epoi
nterposi
ti
ont
oendoff
il
e
si
ze=f
in.
tel
l
g()
;//
countnumberofby
test
il
lcur
rentpost
ionf
orf
il
epoi
nter
f
in.
seekg(
0,i
os:
:
beg)
;//
bri
ngposi
ti
onoff
il
epoi
ntert
obegi
ningoff
il
e
whi
l
e(f
in)
f
in.
get
(ch)
;
i
f(
ch=='
'|
|ch=='
\n'
)
wor
d++;
i
f(
ch=='
\n'
)
l
i
ne++;
}
cout
<<"
\nNo.ofLi
nes="
<<l
i
ne<<"
\n\
nNo.ofWor
ds="
<<wor
d;
cout
<<"
\n\
nSi
zeofTextFi
l
e="
<<si
ze<<"
\n"
;
f
in.
close(
);/
/cl
osi
ngf
il
e
r
etur
n0;
TextFi
l
e1i
sopened.
..
..
..
.
No.ofLi
nes=5
No.ofWor
ds=32
Si
zeofTextFi
l
e=188