Binary files:
Text file is a file where the data of any type is converted
into text and stored.
For example the salary 7560 will be converted into
individual characters and stored as individual characters "7", "5",
"'6" and "0".
When we read into the program then the text type is again
converted into number type.
It takes more memory (7560 takes 2 bytes where as
"7560" takes 4 bytes because every character takes 1 byte)
It shows less performance in writing and reading from the
file (Because data must be converted while writing and reading from the file)
We can save the memory and improve the performance by
writing the binary data directly on to the file.
The file with binary data is called a binary file.
Binary files can't open with any other applications.
fwrite():
It is a function used to write an object (structure
variable) on to the file in a binary format
Writes "n" items of "size" bytes each
through the file pointer
It accepts the address of object, size of object, number
object to be written and the file pointer as arguments.
Returns the number of actually written items (not bytes)
Function definition:
int
fwrite(void *ptr, int size, int n, FILE *fp);
{
-------------------------------------
-------------------------------------
-------------------------------------
}
Example:
Example explained:
- Creating
the file "data1" in binary write mode.
- fwrite()
is used to write the object on to the file in a binary format. Here the
address of object (6332), size of object (44), number of objects (1) and
the file pointer are sending as arguments.
fread():
It is the function used to read the binary data from the
file and store into the object
Reads "n" items of "size" bytes each
through the file pointer
It accepts the address of object, size of object, number
object to be read and the file pointer as arguments.
Returns the number of items fetched from the file (not
bytes)
Function definition:
int fread(void *ptr, int size, int n, FILE *fp);
{
-------------------------------------
-------------------------------------
-------------------------------------
}
Example:
Program explained:
- Opening
the file "data1" in binary read mode
- fread()
is used to read the binary data from the file and write onto the object.
Here the address of object (6332), size of object (44), number of objects
(1) and the file pointer are sending as arguments.
Specification7:
Accept "n" addresses from the keyboard and write
onto the file "data1"
Program:
#include<stdio.h>
struct address
{
char name[20];
char place[20];
long int pin;
};
void main()
{
FILE *p;
struct address x;
int n,i;
clrscr();
p=fopen("data1","wb");
printf("How
many records?");
scanf("%d",&n);
printf("Enter
%d records:\n",n);
for(i=1;i<=n;i++)
{
printf("Name:");
scanf("%s",x.name);
printf("Place:");
scanf("%s",x.place);
printf("Pin
code:");
scanf("%ld",&x.pin);
fwrite(&x,sizeof(x),1,p);
}
fclose(p);
printf("1 file
created");
getch();
}
Execution:
How many records?3
Enter 3 records:
Name:Smith
Place:Kavali
Pin code:524201
Name:John
Place:RudraKota
Pin code:524201
Name:Black
Place:BudamGunta
Pin code:425201
1 file created
Specification8:
Read the data from file "data1" and display onto
the monitor.
Program:
#include<stdio.h>
struct address
{
char name[20];
char place[20];
long int pin;
};
void main()
{
FILE *p;
struct address x;
clrscr();
p=fopen("data1","rb");
printf("Rcords
of file:\n");
while(1)
{
fread(&x,sizeof(x),1,p);
if(feof(p)!=0)
break;
printf("\n\nName:%s\nPlace:%s\nPin
code:%ld",x.name,x.place,x.pin);
}
fclose(p);
getch();
}
Execution:
Rcords of file:
Name:Smith
Place:Kavali
Pin code:524201
Name:John
Place:RudraKota
Pin code:524201
Name:Black
Place:BudamGunta
Pin code:425201
1 file created
fputs():
It is the function used to print a string onto the file.
It accepts both the string and the file pointer as
arguments.
It returns the last character written on to the file if
operation is successful otherwise returns -1
Function definition:
int
fputs(char *s, FILE *p)
{
-------------------------
-------------------------
-------------------------
}
Specification9:
Accept multiple lines of text and write onto the file
"fruits".
Program:
#include<stdio.h>
#include<string.h>
void main()
{
FILE *p;
char x[40];
clrscr();
p=fopen("fruits","w");
printf("Enter
the names of fruits:\n");
printf("exit
to close..\n");
while(1)
{
gets(x);
strcat(x,"\n");
if(strcmp(x,"exit")==0)
break;
fputs(x,p);
}
fclose(p);
printf("1 file
created");
getch();
}
Execution:
Enter the names of fruits:
exit to close..
apple
grapes
jackfruit
exit
Note: Here "exit" is used as terminating word but
not written on the file.
Program explained:
Opened the file "fruits" in write mode
gets() is used to read a line of text from the keyboard
Writing the strings on to the file using fputs() through the
file pointer
strsmp(x,"exit") is used to check for end of file
0 comments:
Post a Comment