Data Structure
Data Structure
In C, a structure is a collection of variables that are referenced under one name, providing a convenient means of keeping related information together. A structure declaration forms a template that may be used to create structure variables. The variables that make up the structure are called structure elements. Generally, all the elements in the structure are related to each other logically. The following code fragment declares a structure template that defines the name and address fields of a mailing list structure. The keyword struct tells the compiler that a structure is being declared. struct addr{ char name[30]; char street[40]; char city[20]; char state[3]; unsigned long int zip; }; The declaration is terminated by a semicolon because structure declaration is a statement. Also, the structure name addr identifies this particular data structures and its type specifier. The structure name is often referred to as its tag. At this point in the code, no variable has actually been declared. Only the form of the data has been defines. To declare an actual variable with this structure, you would write struct addr addr_info; This declares a structure variable of type addr called addr_info. Or you can include it directly from the structure you made. struct addr{ char name[30]; char street[40]; char city[20]; char state[3]; unsigned long int zip; } addr_info; The structure tag name is the name of the structure not the variable name. The structure variables are a comma separated list of variable names. !emember, either structure tag name or structure variables are optional, but not both. !eferencing "tructure #lements
Individual structure elements are referenced by using the . usually the !dot"# operator. $or e%ample, the following code assigns the &ip code '()*+ to the &ip field of the structure variable addr_info declared earlier addr_info.zip $ %234&, The general form if structure-name.element-name.
File Handling
In C//, file input0output operations are implemented through a component header file called fstream.h. The fstream library predefines a set of operations for handling file related input and output. It defines certain classes that help perform file input and output. The important stream classes re1uired for file I0O are. ifstream' is re1uired for input from a file 2reading from a file3. Its member function open( ) associates the stream with a specified file in an input mode. $or e%ample, if a file called xyz.dat is re1uired to be opened for reading, the following statements can be sued. ifstream readfile, readfile.open24%y&.dat53, ofstream' is re1uired for output to a file 2writing to a file3. Its member function open( ) associates the stream with a specified file in an output mode. $or e%ample, if a file called xyz.dat is re1uired to be opened for writing, the following statements can be sued. ofstream writefile, writefile.open24%y&.dat53,
fstream' can be used for simultaneous input from and output to a file. The member functions commonly used are. 1. 2. 3. 4. %. &. '. open close close all seekg seekp tellg tellp : associates the stream with a specified file. : close the stream. : closes all open streams. : Used with inp t file (ifstream). !eeks "yte no. in a data file. #x. fileo t.seekg(3$) will goto "yte n m"er 3$. . !ame as seekg " t sed with o tp t file (ofstream). : ret rns c rrent get position. : ret rn c rrent p t position.
ios''*inary
?ata files can be opened in Te%t mode or @inary 8ode. @y default files are opened in Te%t 8ode. 6hen a file is opened is Te%t 8ode, various character translation may take place such as carriage>return 2enter3 into newline. Aowever, no such character translation takes place in @inary 8ode. Any file can be opened in te%t 8ode or @inary 8ode. The only difference is the occurrence of character translation in Te%t 8ode.
char ans$:y:; clrscr #; ;hile ans$$:<:==ans$$:y:# { cout77endl7793mployee >ame 9; cin.get name?30#; ..read string cout77endl779,alary 9; cin88salary; cin.get ch#; ..empty *uffer enter character# ..;rite to file fileout77name77:@n:77salary77:@n:; cout779@n3nter Aore Becord [<.>] 9; ans$getch #; } fileout.close #; ..close the file ifstream fileinput; ..open employee.dat for reading records fileinput.open 9employee.dat9#; fileinput.see+g 0#; ;hile Cfileinput.eof ##; ..3D0 detects end of file { fileinput.get name?30#; ..read employee name fileinput.get ch#; ..read the eEtra character fileinput88salary; ..read salary fileinput.get ch#; ..read the eEtra character cout77endl7793mployee:s >ame 977name77endl; cout7793mployee:s ,alary 977salary77endl; } fileinput.close #; ..close file }
int total; }; main # { char fname[%&]; ..0or Dutput 0ilename int num*er; ..0or >um*er of ,tudents student srec; ..Feclare ,tructure Garia*le ofstream outfile; clrscr #; cout7793nter the name of the file to *e created 9; cin88fname; outfile.open fname#; ..Dpen the filename in output mode if Coutfile# ..(hec+out for 0ile 3rrors { cout77endl7793BBDBCCC (annot Dpen 0ile9; return %; } cout77endl7793nter the num*er of students in the class 9; cin88num*er; for int i$0;i7num*er;iHH# ..Iet Becords { ..Bead the ,tructure from Jey*oard cout779>ame 9; gets srec.name#; cout779Boll 9; gets srec.roll#; cout779Kotal 9; cin88srec.total; ..)rite to 0ile outfile.;rite char /# Lsrec?sizeof srec##; } ..(lose the 0ile outfile.close #; ..Fisplay Mll the Becords in the 0ile ifstream infile; infile.open fname#; infile.see+g 0#; ;hile Cinfile.eof ## { infile.read char /# Lsrec?sizeof srec##; cout77endl77endl779>ame 977srec.name; cout77endl779Boll 977srec.roll; cout77endl779Kotal 977srec.total; } infile.close #;
..Fisplay Becords
return 0#; }
clrscr #; do { cout779@n 3nter 1nformation '9; std.input #; ..call function input file.;rite char/# Lstd? sizeof std##;..;rite to file cout779@n3nter Aore[y.n]P 9; ..as+ for more info. cin88choice; };hile choice$$:<:==choice$$:y:#; ..file.close #; ..file.open 9student.dat9?ios''app=ios''out?ios''in#; ..open file file.see+g 0#; ..reset the file:s current pointer to starting file.read char /# Lstd? sizeof std##; ..read from file cout779@n,KQF3>K 1>0DBAMK1D> '9; ..display students: information ;hile Cfile.eof ## { std.display #; ..call function display cout77endl; file.read char /# Lstd? sizeof std##; ..read from file } file.close #; getch #; }
get ch#
#%tracts one character from the stream and places it into the variable named ch.
char nameE)CF, cin.get2name,)C3, 0G reads upto (H characters and adds terminating null 2DC3. The Dn remains in the input stream. G0
char nameE)CF,
cin.get2name,)C,I%I3, #%tracts characters from the stream and places them into variable string until either ma%>' characters have been read, the delimiter character has been found, or the end of file is reached. The string is null>terminated by get23, The delimiter character is left in the stream until the ne%t read. 0G reads upto (H characters or until it sees an % in the stream and adds terminating null 2DC3. The Dn or delimiter remains in the input stream. G0 char nameE)CF, cin.getline2name,)C3, 0G reads upto (H characters and adds terminating null 2DC3. The Dn is removed from the input stream. G0
char nameE)CF, cin.getline2name,)C,I%I3, 0G reads upto (H characters or until it sees an % in the stream and then adds terminating null 2DC3. The Dn or delimiter is removed from the input stream. G0
6include 7conio.h8 6include 7fstream.h8 6include 7stdio.h8 6include 7string.h8 6include 7process.h8 6include 7io.h8 int count$0;
char address[30]; char phone[%0]; int unit; pu*lic' Goid getdata Goid#; Goid display Goid#; Goid modify Goid#; }; Goid Kelephone''getdata Goid# { char ch; clrscr #; gotoEy %&?%0#; cout779Nharat ,anchar >igam 2imited R 1nput Becord@n9; gotoEy %S?%2#; cout779Becord 6977 HHcount#77endl; gotoEy %?%4#; cout779@n3nter >ame ' 9; cin.getline name?30#; cout779@n3nter Mddress ' 9; cin.getline address?30#; cout779@n3nter -hone >o. ' 9; cin.getline phone?30#; cout779@n3nter Qnit ' 9; cin88unit; } Goid Kelephone''display Goid# { clrscr #; gotoEy %&?%0#; cout779Nharat ,anchar >igam 2imited R Fisplay Becord@n9; gotoEy %?%2#; cout779@n>ame ' 977name; cout779@nMddress ' 977address; cout779@n-hone >o. ' 977phone; cout779@nQnit ' 977unit; }
Goid Kelephone''modify Goid# { char tname[30]; char taddress[30]; char tphone[%0]; int tunit; clrscr #; gotoEy %&?%0#; cout779Nharat ,anchar >igam 2imited R Aodify Becord@n9; gotoEy %S?%2#; cout779@n(urrent Fetails@n9; gotoEy %?%2#; cout779@n>ame ' 977name; cout779@nMddress ' 977address; cout779@n-hone >o. ' 977phone; cout779@nQnit ' 977unit; cout779@n3nter >e; Fetails 977endl; cout779@n3nter >ame ' 9; cin.getline tname?30#; cout779@n3nter Mddress ' 9; cin.getline taddress?30#; cout779@n3nter -hone >o. ' 9; cin.getline tphone?30#; cout779@n3nter Qnit ' 9; cin88tunit; strcpy name?tname#; strcpy address?taddress#; strcpy phone?tphone#; unit$tunit; } Goid main Goid# { clrscr #; Kelephone tel; fstream fileout; fileout.open 9Kel.dat9?ios''in=ios''out=ios''app#;
if Cfileout# { cout779(annot Dpen 0ile9; eEit 0#; } int choice; int mrec$0; int offset$0; char ans?ch; do { clrscr #; cout779@n@nNharat ,anchar >igam 2imited9; cout779@n@n@t@tAain Aenu 9; cout779@n@n@t%. Mdd Becord 9; cout779@n@t2. Aodify Becord 9; cout779@n@t3. Fisplay Becord 9; cout779@n@t4. 3Eit 9; cout779@n@n@3nter <our (hoice [%R4] 9; cin88choice; cin.get ch#; ..empty *uffer enter character# s;itch choice# { case %' tel.getdata #; mrec$count; offset$ mrecR%#/sizeof Kelephone##; fileout.see+p offset?ios''*eg#; fileout.;rite char /#Ltel?sizeof Kelephone##; *rea+; case 2' if Ccount# { cout779>o records found.@n9; cout779@n-ress a +ey to continue...9; getch #; *rea+; } cout779@nAodify ;hich recordP 6 9; cin88mrec; cin.get ch#; ..empty *uffer enter character# if mrec8count# { cout7791nGalid record no.@n9; cout779@n-ress a +ey to continue...9;
getch #; *rea+; } else { offset$ mrecR%#/sizeof Kelephone##; fileout.see+g offset?ios''*eg#; fileout.read char /#Ltel?sizeof Kelephone##; tel.display #; cout779@nAodify this record [y.n] 9; cin88ans; cin.get ch#; ..empty *uffer enter character# if ans$$:<: == ans$$:y:# { tel.modify #; fileout.see+p offset?ios''*eg#; fileout.;rite char /#Ltel?sizeof Kelephone##; cout779@nBecord Aodified9; cout779@n-ress any +ey to continue...9; getch #; } *rea+; } case 3' if Ccount# { cout779>o records found.@n9; cout779@n-ress a +ey to continue...9; getch #; *rea+; } fileout.see+g 0#; fileout.read char /#Ltel?sizeof Kelephone##; cout779@nB3(D-BF >D. 69; tel.display #; do { fileout.read char /#Ltel?sizeof Kelephone##; cout779@n-ress a +ey to display neEt record9; getch #; tel.display #; };hile Cfileout.eof ##; cout779@n-ress any +ey to continue...9; getch #;
*rea+; case 4' [email protected]; cout779@n-ress a +ey to continue...9; getch #; eEit 0#; default'cout779@n@n@a@a@a1nGalid choice9; *rea+; } };hile choice8$% LL choice 7$4#; fileout.close #; }