Block read/write
It is useful to store the block of data into the file rather than individual elements.Each block has some fixed size,it may be of strcture or of an array.It is possible that a data file has one or more structures or arrays,So it is easy to read the entire block from file or write the entire block to the file.There are two useful functions for this purpose
1. fwrite():
This function is used for writing an entire block to a given
file.
Syntax: fwrite( ptr, size, nst, fptr);
Where ptr
is a pointer which points to the arrayof struture in which data
is written.
Size is the size of the structure
nst
is the number of the structure
fptr is a filepointer.
Example program for fwrite():
Write a program to read an
employee details and write them into the file at a time using
fwrite().
#include<stdio.h> #include<conio.h> void main() { struct emp { int eno; char ename[20]; float sal; }e; FILE *fp; fp=fopen("emp.dat", "wb"); clrscr(); printf("Enter employee number"); scanf("&d",&e.eno); printf("Enter employee name"); fflush(stdin); scanf("%s",e.ename); printf("Enter employee salary"); scanf("%f",&e.sal); fwrite(&e,sizeof(e),1,fp); printf("One record stored successfully"); getch(); }
OUTPUT:
Enter employee num: 10
Enter employee name : seshu
Enter employee salary : 10000
One record stored successfully.
2. fread():
This function is used to read an entire block from a given file.
Syntax: fread ( ptr , size , nst , fptr);
Where ptr
is a pointer which points to the array which receives structure.
Size is the size of the structure
nst is the
number of the structure
fptr is a filepointer.
Example program for fread():
Write a program to read an
employee details at a time from a file using fread() which are
written above using fwrite()and display them on the output
screen.
#include<stdio.h> #include<conio.h> void main() { struct emp { int eno; char ename[20]; float sal; }e; FILE *fp; fp=fopen("emp.dat", "rb"); clrscr(); if(fp==NULL) printf("File cannot be opened"); else fread(&e,sizeof(e),1,fp); printf("\nEmployee number is %d",e.eno); printf("\nEmployee name is %s",e.ename); printf("\nEmployee salary is %f",e.sal); printf("One record read successfully"); getch(); }
OUTPUT:
Employee number is 10
Employee name is seshu
Employee salary is 10000.
One record read successfully.
fprintf ()
This function is same as the printf() function but it writes the
data into the file, so it has one more parameter that is the
file pointer.
Syntax: fprintf(fptr,
"controlcharacter",variable-names);
Where fptr is a
file pointer
Control character specifies the type of
data to be printed into file.
Variable-names hold the
data to be printed into the file.
fscanf ()
This function is same as the scanf() function but this reads the
data from the file, so this has one more parameter that is the
file pointer.
Syntax: fscanf(fptr, "control
character", &variable-names);
Where fptr is a
file pointer
Control character specifies the type of
data to be read from the file.
Address of Variable names
are those that hold the data read from the file.
Example for fprintf() and fscanf()
Write a program to
read details of an employee and print them in a file using
fprintf() and read the same from the file using fscanf() and
display on the output screen.
#include<stdio.h> #include<conio.h> void main() { FILE *fp; int eno; char ename[20]; float sal; clrscr(); fp=fopen("emp.dat", "w+"); printf("Enter employee number"); scanf("&d",eno); printf("Enter employee name"); fflush(stdin); scanf("%s",ename); printf("Enter employee salary"); scanf("%f",sal); fprintf(fp, "%d %s %f\n",eno,ename,sal); printf("Details of an employee are printed into file"); rewind(fp); fscanf(fp,"%d %s %f",&eno,ename,&esal); printf("Details of employee are\n"); printf("\n %d %s %f", eno,ename, sal); fclose(fp); getch(); }
OUTPUT:
Enter employee num: 10
Enter employee name : seshu
Enter employee salary : 10000
Details of an employee are printed into file.
Details of employee are
Employee number is 10
Employee name is seshu
Employee salary is 10000.
feof()
The macro feof() is used for detecting whether the file pointer
is at the end of file or not.It returns nonzero if the file
pointer is at the end of the file otherwise it returns zero.
Syntax: feof(fptr);
Where fptr is a file pointer .
ferror()
The macro ferror() is used for detecting whether an error occur
in the file on filepointer or not.It returns the value nonzero
if an error,otherwise it returns zero.
Syntax:
ferror(fptr);
Where fptr is a file pointer.