FILE ACCESSING TECHNIQUES (or) FILE TYPES
Every
open file has an associated file position indicator, which describes where read
and write operations take place in the file.
The position is always specified in bytes from the beginning of the
file.
When
a file is opened in either read (or) write mode, the position indicator is
always at beginning of the file i.e., at position ‘0’. If the file is opened in append mode, the
position indicator is at the end of the file.
File accessing techniques can be classified
into two types as:
1.
Sequential
file processing
2.
Random
access file processing
1. Sequential File Processing: In sequential
file processing, the file pointer moves character by character without skipping
data. i.e., read or write operations performed sequentially. getc(), putc(), fgets(), fputs() etc.,
functions support sequential file processing.
2. Random Access File Processing: In
random access file processing, the file pointer can change from one location to
another location according to user requirements. Here, operations are performed in random
access manner. Most important functions
used in random access file processing are:
a) fseek()
b) ftell()
c) rewind()
a) fseek() function: fseek() function is used
to move the position indicator to a desired location within the file. The general format of the fseek() function
is:
Syntax: fseek(FilePointer,
OffSet, Position);
Where,
Ø
OffSet
is the number of bytes to be moved and it must be long integer. The value may be positive of negative. If the OffSet value be positive, file pointer
moves to forward direction; otherwise, file pointer moves to backward
direction.
Ø
Position
specifies the starting position in the file to move. Position can take any one of the following
three values.
VALUE
|
MEANING
|
0
|
Beginning
of File
|
1
|
Current
Position
|
2
|
End of
File
|
When the operation is successful, fseek()
function returns zero; otherwise, it return -1.
Examples:
1. fseek(fp,0L,0) - Go
to beginning of the file
2. fseek(fp,0L,1) - Stay
at the current position
3. fseek(fp,0L,2) - Go
to end of the file
4. fseek(fp,m,0) - Move
to m bytes from beginning of the file
5. fseek(fp,m,1) - Go
to forward by m bytes from current position
6. fseek(fp,-m,2) - Go
to backward by m bytes from the end of file.
b) ftell() function: ftell() function is used
to return the current position of the file pointer in the file. The general format of the ftell() function
is:
Syntax: N
= ftell(FilePointer);
Where,
N
is a long integer variable.
Function returns current position (in bytes) as
long integer value. If any error
encountered, then the function returns -1.
c) rewind() function: rewind() function is used to reset the file pointer to
beginning of the file. The general
format of the rewind() function is:
Syntax: rewind(FilePointer);
Function reset the file pointer at the
beginning of the file.
WRITE A PROGRAM TO PRINT
THE CONTENTS OF A FILE IN REVERSE ORDER
#include<stdio.h>
main()
{
FILE
*fp;
long
n,i;
char
ch;
clrscr();
fp=fopen("den.txt","r");
if(fp==NULL)
{
printf("\nFile
Opening Error");
exit();
}
fseek(fp,-1L,2);
n=ftell(fp);
printf("\nFile
Contents in Reverse Order:\n");
for(i=1;i<=n+1;i++)
{
fseek(fp,-i,2);
putchar(fgetc(fp));
}
fclose(fp);
}
No comments:
Post a Comment