Saturday, February 7, 2015

IMPLEMENTATION OF RANDOM ACCESS FILE

AIM
            To write a C program to implement random access of files.

ALGORITHM
Step-1: Start the program
Step-2: Open the file RANDOM in write mode and get the characters.
Step-3: If it is the End of File then print the number of characters entered.
Step-4: Close the file and open it in read mode.
Step-5: Find the position of each character using fseek and print it.
Step-6: Stop the program

PROGRAM
#inlcude<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
long n;
char c;
fp=fopen(“RANDOM”,”w”),;
while((c=getchar())!=EOF)
putc(c,fp);
printf(“no. of characters entered = %ld \n”,ftell(fp));
fclose(fp);
fp=fopen(“RANDOM”,”r”);
n=OL;
while(feof(fp)==0)
{
fseek(fp,n,0); /* position to ()n+1)th character */
printf(“Position of %c is %ld \n”, getc(fp),ftell(fp));
n=n+5L;
}
putchar(‘\n’);
fseek(fp,-1L,2);
do
{
putchar(getc(fp));
}
while(!fseek(fp,-2L,1));
fclose(fp);
}




OUTPUT
ABCDEFGHIJKLMNOPQRSTUVWXYZ

No. of characters entered =26
Position of A is 0
Position of F is 5
Position of K is 10
Position of P is 15
Position of U is 20
Position of Z is 25

  
RESULT: Thus a C program is written to implement random access of files is executed.

No comments:

Post a Comment