c programs on file handling, c programs for file handling, c program on file handling, file handling in c programs, file handling using c, c program for creating a file,what is header file in c programming, c program for file operations ,Explain Random Access to file in C, sequentially,Randomly,
Explain Random Access to file in C expain fseek( ) Function ftell ( ) Function with example ,
SEEK_SET: origin is the start of the stream
SEEK_CUR: origin is the current position
EEK_END: origin is the end of the stream
Explain Random Access to file in C
Random access means we can move to any part of
a file and read or write data from it without having to read through the
entirefile. we can access the data stored in the file in two ways.
1. sequentially 2.Randomly
if
we want to access the forty fourth record then first forty three record read
sequentially to reach forty four record .In random access data can be
accessed and processed directly .There is no need to read each record
sequentially .if we want to access a particular record random
access takes less time than the sequential access.
C
supports these function for random access file.
1.fseek(
) Function
2.ftell
( ) Function
How to use fseek() function in C:
This
function is used for setting the file position pointer at the specified bytes .fseek
is a function belonging to the ANCI C standard Library and included in
the file stdio.h. Its purpose is to change the file position indicator for the
specified stream.
Syntax
:intfseek(FILE*stream_pointer,
longoffset, intorigin);
Argument
meaning:
- stream_pointer is a pointer to the stream FILEstructure of which the position indicator should be changed;
- offset is a long integer which specifies the number of bytes from origin where the position indicator should be placed;
- origin is an integer which specifies the origin position. It can be:
- SEEK_SET: origin is the start of the stream
- SEEK_CUR: origin is the current position
- SEEK_END: origin is the end of the stream
/* Program to understand the use of fseek function : */
#include
#include
#include
structemprecord
{
char name[30];
int age;
floatsal;
}emp;
void main()
{
int n;
FILE *fp;
fp=fopen("employee.dat","rb");
if (fp==NULL)
{
printf("/n error in
opening file");
exit(1);
}
printf("enter the
record no to be read");
scanf("%d",&n);
fseek(fp,(n-1)*sizeof(emp),0);
freed(&emp,sizeof(emp),1,fp);
printf("%s\t,emp.name);
printf("%d\t",emp.age);
printf("%f\n",emp.sal);
fclose(fp);
getch();
}
How
to use ftell() Function in C:
This
function return the current position of the file position pointer. The value is
counted from the beginning of the file.
Syntax
: long ftell (file * fptr);
/*program
to understand the use of ftell ( )/*
#include
#include
#include
structemprecord
{
char name[30];
int age;
floatsal;
}emp;
void main()
{
FILE *fp;
fp=fopen("employee.dat","rb");
if (fp==NULL)
{
printf("/n error in
opening file");
exit(1);
}
printf("position
pointer in the beginning -> %1d ",ftell(fp));
while("fread(position
pointer -> 1%d\n",ftell(fp));
printf("position
pointer -> %1d\n",ftell(fp));
printf("%s\t",emp.name);
printf("%d\t",emp.age);
printf("%f",emp.sal);
}
printf("size of the
file in bytes is %1d\n",ftell(fp));
fclose(fp);
getch();
}