17.12 – Random Access or Direct Access files

by subbu on October 17, 2014

Random access/Direct access files:

In general we access the data from a file character by character or record by record from the beginning of the file to the end of file called sequential access. If the size of file is so small then takes reasonable time to get the required record but takes time if a file has 1000’s of records.

Say for example, a file has 10000 records and has to get 9999th record then we have to all the way go through 9999 records to get the required record, results take a lot of time.

We can overcome this by directly accessing a record from the required position. It can be done in two steps that are, sending the file active pointer to the required position using fseek() and reading the record using fread() as we learned in the previous session.

fseek() function:

It is the function used to send the file pointer to the specified position from the specified position.
fseek() function

According to the function definition, it accepts three arguments those are

  1. File pointer
  2. “offset” specifies the number of bytes to be skipped to locate a record
  3. “Whence” specifies from where “offset” is measured.
  4. “whence” must be 0 if we want to specify the beginning of file (BOF)

It returns zero on successfully reaching to the required position otherwise returns a non-zero

Note: Random or direct access can be performed only on binary files

Let us see an example to get more clarity

Using fseek() in C language

  1.  Sends the file pointer from 0 to 0 that is the first record.
  2. Sends the file pointer from 0 to 28 that is the second record.
  3. Sends the file pointer from 0 to 56 that is the third record

” (n-1)*sizeof(var)” is to send to the “nth” record

 Specification: Accept “n” records from the keyboard, write onto the file “employee” and display the required record using direct or random access.

#include<stdio.h>
struct emp
{
 int empno;
 char ename[20];
 int sal;
 long int phno;
};
int main()
{
 struct emp x;
 FILE *p;
 int n,i,t,rec;
 char choice[1];
 p=fopen("employee","wb+");   /* opening the file in binary write/read mode*/
 printf("How many records?");
 scanf("%d",&n);
 printf("Enter %d records:\n",n);
 for(i=1;i<=n;i++)
 {
   printf("\n\nEmpno:");
   scanf("%d",&x.empno);
   printf("Name:");
   scanf("%s",x.ename);
   printf("Salary:");                       /* Accepting the data from keyboard*/
   scanf("%d",&x.sal);
   printf("Phone No:");
   scanf("%ld",&x.phno);
   fwrite(&x,sizeof(x),1,p);             /* Writing objects onto the file*/
 }
 printf("Press any key to continue....\n");
 getchar();                                     /* To wait for user confirmation*/
 while(1)
 {
   printf("\nEnter the record number:");
   scanf("%d",&rec);                           /* Accepting record number */
   if(rec<1||rec>n)                            /* Checking for validity of record number*/
    printf("Search failure...");
   else
   {
    fseek(p,(rec-1)*sizeof(x),0);   /* placing record pointer to the required record*/   
    fread(&x,sizeof(x),1,p);               /* Reading the record*/
    printf("Empno:%d\nName:%s\nSalary:%d\nPhno:%ld",x.empno,x.ename,x.sal,x.phno); /* Printing the record*/ 
 }
 printf("\nWant to continue...y/n:");   /* accepting confirmation to continue*/
 scanf("%s",choice);
 if(strcmp(choice,"n")==0)
   break;
}
fclose(p);
return 0;
}

Execution:
How many records?3
Enter 3 records:

Empno:1000
Name:Black
Salary:2700
Phone No:56789

Empno:1001
Name:Smith
Salary:3650
Phone No:56788

Empno:1002
Name:Rajesh
Salary:5400
Phone No:98765

Press any key to continue….

Enter the record number:0
Search failure…
Want to continue…y/n:y

Enter the record number:2
Empno:1001
Name:Smith
Salary:3650
Phno:56788
Want to continue…y/n:y

Enter the record number:1
Empno:1000
Name:Black
Salary:2700
Phno:56789
Want to continue…y/n:y

Enter the record number:6
Search failure…
Want to continue…y/n:n

Previous post:

Next post: