Random Access of Files in C

Preview
You must first complete File Input output in C before viewing this Lesson




Random Access of Files in C

In C language, we can randomly position file pointer at a particular position in the file and can perform reading and writing of data at that particular position. There are two predefined functions provided by C language to randomly position the file pointer at a particular position in the file. These are as follows:

  • fseek()
  • ftell()

1. fseek() function

This function is used to position the file pointer inside the file at a particular position in the file. By positioning the file pointer at a particular position inside the file we can read or write a particular character or word in the file. The syntax of fseek() is:

fseek(Filepointer, Offset, Origin);

Filepointer refers to the file pointer variable which is used to refer to a file from which we want to read the data.

offset is a long integer value which specifies the number of bytes from origin where the position indicator should be placed.

Origin is an integer value which specifies the origin position. It can have following values
SEEK_SET:  origin is the start of the stream. We can also write 0 here.

SEEK_CUR: origin is the current position.  We can also write 1 here.

SEEK_END: origin is the end of the stream. We can also write 2 here. 

Program to demonstrate the use of fseek() function.
#include<stdio.h>
int main()
{
FILE *f;
char sentence[50];
char ch;
int n;
f=fopen(“contents.text”,”r”);
fgets(sentence,50,f); 
//To read a string value from a fileprintf(“\nContents of file are=”);
printf(“%s”,sentence); 

fseek(f,3,0);
/*File pointer is placed at the 4th character from beginning*/

ch=getc(f);
printf(“\n%c”,ch);

fseek(f,3,1);
/*File pointer is placed at the 4th character from current
position*/

ch=getc(f);
printf(“\n%c”,ch);

fseek(f,-1,2);
/*File pointer is placed at the Last character from the end*/

ch=getc(f);
printf(“\n%c”,ch);

fclose(f);
return(0);
}

Output
Contents of file are=i love india
o
i
a




 2. ftell() function

This function is used to know the position of file pointer in the file. It returns the file pointer in terms of byte number of the file pointer in the file.

Syntax of ftell() function is

ftell(Filepointer)

filepointer is the name of file pointer with which a file has been opened. 

 Program to demonstrate the use of ftell() function.
#include<stdio.h>
int main()
{
FILE *f;
char sentence[50];
char ch;
int n;
f=fopen(“contents.text”,”r”);fgets(sentence,50,f); 
//To read a string value from a fileprintf(“\nContents of file are=”);
printf(“%s”,sentence);fseek(f,-1,2);
/*File pointer is placed at the Last character from the
end*/n=ftell(f);  
/*Position of file pointer is stored in variable n.*/printf(“\nFile pointer is at position %d”,n);
fclose(f);
return(0);
} 
Output
Contents of file are=i love india
File pointer is at position 11




Best Books of C

Lesson tags: File pointer and random access IN C, fseek() function in C, ftell() function in C, Random Access of Files in C
Back to: C Programming Language