Assignment 5 Cse
Assignment 5 Cse
Unions in
Unions, ke stwctwe, allo gou to gop vasiabley
of dffoent dat yres.
wcli memosy as the ayest
ynemben, wheeas s4wtes
) e memory egual to fhe dm
of hein menbers.
ymember of a ion Cau be
accesÇe d t a
tme
Declation
Unen Value
flont foatnlue;
float floatalue ;
chs sthyvalue [2o3;
compla inmplameatadie
corpred to tequenhal fles.
05 fle in C?
Houw you Ceate a clata
Explin the pro cets
dote 7le jn C mres the fle .
wite nead and lee
9ndown:
baic
data fle in C:
Steps to Create a
foren ("4lename.txt""w");
fon witg 34 the file alneny
Ymode Csietes a new fle
contents .
exts -
Loil ovEwste i
mode ted to exiating fle.
" "a"
4 Wite clat fprintt ()' n fptel)':
to the fle
data.ln"J;
fprintt (flkfcinte,Ths is one
l on using fpute :
flefointen);
I| fputc ('H',Alelointen );
I| fpute ('i,
felose ()' :
lose he Ale Using
fclore (-fleloindend;
S4 esetal to dose he file aftor you ae done.
from it. This
sICnding sared and 1er0wces neleased.
Exgmple
incuele <stolio. h>
jvet man ()
FILE *flefointerj
"w");
flefointen = foren (" data.txt",
if (flefoinden
peunct ("hle could not be opened."J;
Some dataln"):
teintt ( flelointen, "Thit is
fle in C is sirple!\n"
fpointt ( flefointen ,"Wstig to a
fclose (fle Pointen;
"Puopore
Synt
To cacl clata frem an opencd tile
Witing
fprintf (),
Data to 9 fle :
fputs (), fwnitel.
furpose- To write data to an opened fle.
" Syntax
"int fpintf( FiLE *flefoider, const chan *format,- );
" int fputs ( cot ch *s4, fILE
**lelointer);
" Sizet fwrte ( cowt void *pta, i2e-t size, G2et nnonb,
FILE *ktelointor
fprintflU wites formated data, fput () wnites a
block of data.
Moing within a file:
fseekl), reindJ
Rnpoe the move
fle.
the fle polnter to a tpe
eie locaton withn
Synty
.jnt fseck (FILE * kfomter ,
" yoid ewind Long offset ,mt whence);
(fILE *fle Poircde:
fseekl) allow ndom Qccest
baed ou
9eindl) move
'henee' Cstt, cnet, on end of ifle).
opecified poiton
the pointen to
the begtnng of the fle.
defnes a
C Pogam that Mwcte
huelent jnfoaon (name, roll unben
to repesent
ama maes ound ntorcs t in a file.
Au ttinclude <stdio.hs
# include < stellib.h>
stvct Studet
chas name (so];
flot j
ivt ymain(){
strwct stvotet stutect ; || Creade a vawahe of type student
I| Get stvelent infomatian forom tue uyer
poivtf ("Eter stolenct ane ; ";
fgett (student. name, i2e of (student.mame), stin);
p9untt "te oll mumter: "},
Scant (od", k studet. oll Mnben );
privet f (" ter ks:");
scant (%d", stvdet. maksJ;
a le in wtt noee
FILE *6lefoinen;
flefointen - fopen ( "studet info. 4xt", "w;
Il check if the file was openet fucestly
if (AlePoindea= NULL)
pritt ("sovon openng te tle\n");
-PSynt
uopoe clata forom opencd file
Woutfprintt
ing Data(),
to q fle :
fputs (), fuwnitel ).
" fupose- To write data to an pencd fle.
" Syntax
"int fpnt f ( fILE *flefonder , cont chan tformat,J;
tpucs Cconst cha *st, FILE **lefointer );
" Si2e_t furte(cowt void *ptr, Gize-t size, Gi2e-t nnonb,
FILE *flefoite
fporintfl wnites toratked data , fput() wtes a
a block of data.
Moving within file:
fseek(), ewin0
Pupoe- o more te fle pointe to a tpe ci fie
the fle. Locatton wihn
t fsek (FILE *fefoden ,
" yoid Long offset ,t whcnce);
rewind (FILE *flefojvden
fseekl) llow andem accet
baed ow
lwhenee' by mog t a
reind() mmoes theCstut, cnet, on end of pecited poition
pojndes to the fle).
beginng of the fte.
Il clote te fle
fclore (fle Pointen);
fclore (leforntea );
3
pPrgam that eads a teot fle hd
Q8. Ceate
Cont the wbe of
#incltde <stelo. h>
# defne MAX_woRDLENhTH Go
FILE * fteloivden ;
lhr fleNane Cso];
clhan wod [MAy-wORD. LEN6THJ;
jvt wod Cotz0,
pivtt ( Enter he mame ot te file ");
Scantl s , ile Name))