436 Programming and Data Structures
13.5 STRUCTURES READ AND WRITE
It is important to know how numerical data is stored on the disk by f p rin t f () function. Text and
characters require one byte for storing them with f print f ( ) . Similarly for storing numbers in memory
two bytes and for floats four bytes are required.
All data types are treated as characters for example the data 3456, it occupies two bytes in memory.
But when it is transferred to the disk file using f p rin t f () function it would occupy four bytes. For
each character one byte would be required. Even for float also each digit including dot ( . ) requires
one byte. For example 12.34 would require five bytes. Thus, large amount of integers or float data
requires large space on the disk. Hence in the text mode storing of numerical data on the disk turns
out to be inefficient. To overcome this problem the files should be read & written in binary mode, for
which we use functions f read () & fw ri te ().
1 fw r ite ( ) & fre ad ( )
1) fw ri te () This function is used for writing an entire structure block to a given file.
2) f read () This function is used for reading an entire block from a given file.
Below given are examples of these functions.
13*17 Write a program to write a block of structure elements to the given file using fwriteO
function*
# include <Btdio.h>
# include <con±o»h>
# include <proceas.h>
void main ()
{
struct
{
char name [20];
int age;
} stud[5];
PILE *fp;
int i, 0,n;
char str [15] >
printf ("Enter thefile name
scanf ("% s ",s tr );
fp=fopen(str/'rb);
if(fp = = N U L L )
{
printf ("File dose not exist
exit(l);
I
else
{
printf ("H o w Many Records
scanf (/J%d",&n);
Files 437
for (i=Op<n,i++)
printf ("Name
scanf ( %s",&stud[i].name);
printf ("Age
scanf ("%d",&studli]Mge);
)
while (j<n)
I
fwrite (&stud,sizeof(stud),l,fp);
/++;
/
/
fclose(fp);
/
OUTPUT:
How Many Records: 2
Nam e: SANTOSH
Age : 22
Nam e: AMIT
Age : 14
Explanation In the above program a file is opened in write mode. After successfully opening the
file number of records to be entered is asked. Using the fo r loop records are entered. Using the
while loop and fw rite () statement within it records are written in the file.
13.18 Write a program to write and read the information about the player containing players
name,-age and runs. Use freadO & fwrite<) functions.
# Include <stdio.h>
# include cconio. h>
# include cprocess.h>
struct record {
char player [20];
int age;
int runs;
>»
void main ()
{
FILS p;
struct record amp;
fp«fopen ("record.dat", "w");
i f (fp-*NQLL)
{
printf ("nCan not open the file " ) ;
438 Programming and Data Structures
«d .t (l)»
}
clrscr ();
printf("n Enter Player Name, Age & Runs Scored n");
printf ("= = = = ===== ==== === = ========== W ');
scpnfVAs %d %d&np.player, &emp.age,&emp.runs);
fwrite (&entptfzeof(emp),l,fp);
fcloseffp);
if (tfPrfopenCrecord.dat", ‘Y '))= = N U L L )
i
printf ( Errerin openingfile’);
exiHl);
i
printf ("n Record Entered is n");
fread (&emp,sizeof(entp),l,fp);
printf ( %s %d %d", emp.player, emp.age, emp.runs);
fcloseffp);
}
OUTPUT;
Enter Player Name, Age & Runs Scored
Sachin 2510000
Record Entered is
Sachin 25 10000
Explanation In the above program user writes the information of the player using fw ri te function.
The entire record of player which is containing information given in the structure is written first using
fwrite () function.
Similarly, the information written in the file can be read by using fread () function. Thus, the
program reads entire data file with single fread () and writes the data with single fw rite () function.
These two functions are efficiently used for handling I/O files in comparison to f scanf () and
fprintf ().
13.19 Write a program to write a block of structure elements to the given file using fwriteO
function. User should press' Y' to continue and 'N ' for termination.
# include <atdio.h>
# include <conio.h>
# include <procese.h>
void main ()
{
Files 439
FILE *fp;
char xxexttt'Y' /
struct bike
{
char name [40];
int avg;
float cost;
b
struct bike e;
fpcfopen("bk.txt*,"wb") >
i f (fp»-N0LL)
{
puts("Cannot open f i l e " ) ;
SDd.t(l);
}
clrscrO ;
while (n ext=='Y)
printf ("nModel Name, Average, Prize: ");
scanf ("% s %d %f',e. name, &e.avg,&e.cost);
fwrite(&e,sizeof(e), l,fp);
printf ("nAdd Another (Y /N :”);
fflush(stdtn);
next=getche();
}
fcloseffp);
OUTPUT;
Model Name, Average, Prize: H O N D A 80 45000
Add Another (Y/N: Y
Model Name, Average, Prize: SUZUKI 65 43000
Add Another (Y/N :Y
Model Name, Average, Prize: Y AM A H A 55 48000
Add Another (Y/N: N
Explanation The above program is the same as previous one. Here, the user has to press 'Y ' to
continue and 'N' to stop the program. After every key press 'Y ' and a new record is added to the file.
13.20 Write a program to read the information about the bike like name, average and cost
from the file using freadO function.
# include <stdio.h>
# include <conio.h>
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228