Pages

FILES UNIT 17 PART 2


DIRECT ACCESS FILES
Introduction
A direct access file is a file in which any single component may be accessed at random. Every component has a key value associated with it. A write operation takes a component and its key value, writes the component into the file, and stores both the key and the location of the record in a file, an index. A read operation takes the key of the desired component, searches the index to find the location of the component, and retrieves the component from the file.
Program
A complete C program implementing a direct access file is given below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 50

typedef struct
{
    char name[10];
    int key;
}   file_record;
/* this function adds the relative address to the index for a key */
void create_index(long index[], int key, long rel_add )
{
    index[key] = rel_add;
}

/* this function writes a record to the file */
void write_rec(FILE *fp, file_record rec)
{
    fwrite(&rec,sizeof(rec),1,fp);
}

void main()
{
    long rel_add;
    int key;
    file_record frec;
    long index[MAX];/* an index list*/
    int n,i;

    FILE *recfile=NULL,*ifile=NULL;
    /* this initializes the index list to all -1 */
    for(i=0; i< MAX; i++)
        index[i]= (-1);

    recfile=fopen("mfile","w");
    if(recfile == NULL)
    {
        printf("Error in opening file mfile\n");
        exit(0);
    }
    rel_add = 0 ;
    do
    {
        printf(
" Enter the data value and the key of the record to be added to file mfile\n");
        scanf("%s %d",frec.name,&frec.key);
        while(index[frec.key] != (-1))
        {
              printf(
" A record with this key value already exist in a file enter record key value\n");
              scanf("%s %d",frec.name,&frec.key);
        }
        create_index(index,frec.key,rel_add);
        write_rec(recfile,frec);
        rel_add = ftell(recfile);
        /* this sets the relative address for the next record to be
       the value of current file position pointer in bytes from
       the beginning of the file */
        printf("Enter 1 to continue adding records to the file\n");
        scanf("%d",&n);
    }while(n == 1);
    ifile=fopen("index_file","w");
    if(ifile == NULL)
    {
        printf("Error in opening file index_file\n");
        exit(0);
    }
    fwrite(index,sizeof(index),1,ifile);/*writes the complete index into the index_file */
    fclose(recfile);
    fclose(ifile);
    printf("Enter 1 if you want to retrieve a record\n");
    scanf("%d",&n);
    if( n == 1)
    {
        ifile=fopen("index_file","r");
               if(ifile == NULL)
               {
                      printf("Error in opening file index_file\n");
                      exit(0);
               }
               fread(index,sizeof(index),1,ifile);
               /*reads the complete index into the index list from the
        index_file*/
               fclose(ifile);
          recfile=fopen("mfile","r");
          if(recfile == NULL)
               {
                      printf("Error in opening file mfile\n");
                      exit(0);
               }
    }
    printf("THE CONTENTS OF FILE IS \n");
    while( (fread(&frec,sizeof(frec),1,recfile)) != 0)
    printf("%s %d\n",frec.name,frec.key);
    do
    {
        printf("Enter the key of the record to be retrieved\n");
        scanf("%d",&key);
        rel_add = index[key]; /* gets the relative address of the record from index list */
        if( (fseek(recfile,rel_add,SEEK_SET))!= 0)
        {
               printf("Error\n");
               exit(0);
        }
        fread(&frec,sizeof(frec),1,recfile);
        printf("The data value of the retrieved record is %s\n",
        frec.name);
        printf("Enter 1 if you want to retrieve a record\n");
        scanf("%d",&n);
    } while(n == 1);
    fclose(recfile);
}
Explanation
  1. This program writes the names in the file. A unique integer value is assigned to every name as a key value.
  2. The program takes the name to be stored in the file along with its key value, writes the name and key value in the file, obtains the relative address of that record, and stores it in a list called an index. The index is organized by key value.
  3. When the addition process ends, it writes the complete index into an index file.
  4. When retrieval of names is requested, the following occurs:
    1. The complete index is loaded into a list index from the index file.
    2. The index file is used to find the relative address of the record whose key value is given.
    3. The current position pointer is moved to that address.
    4. The record is read from the file.
Example
Input and Output
1.
Enter the data value and the key of the record to be added to file
mfile
logk 10
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
psd 20
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
apg 3
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
agk 5
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
kdk 34
Enter 1 to continue adding records to the file
0
Enter 1 if you want to retrieve a record
1
THE CONTENTS OF FILE IS

  • 1ogk 10
  • psd 20
  • apg 3
  • agk 5
  • kdk 34


Enter the key of the record to be retrieved
5
The data value of the retrieved record is
agk
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
10
The data value of the retrieved record is
1ogk
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
34
The data value of the retrieved record is
kdk
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
20
The data value of the retrieved record is
psd
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
10
The data value of the retrieved record is
1ogk
Enter 1 if you want to retrieve a record
0
2.
Enter the data value and the key of the record to be added to file
mfile
logk 10
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
psd 20
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
kdk 20
A record with this key value already exist in a file enter record key value
kdk 30
Enter 1 to continue adding records to the file
1
Enter the data value and the key of the record to be added to file
mfile
asg 10
A record with this key value already exists in a file, enter record key value
asg 15
Enter 1 to continue adding records to the file
0
Enter 1 if you want to retrieve a record
1
THE CONTENTS OF FILE IS

  • logk 10
  • psd 20
  • kdk 30
  • asg 15


Enter the key of the record to be retrieved
20
The data value of the retrieved record is
psd
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
15
The data value of the retrieved record is
asg
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
10
The data value of the retrieved record is
logk
Enter 1 if you want to retrieve a record
1
Enter the key of the record to be retrieved
30
The data value of the retrieved record is
kdk
Enter 1 if you want to retrieve a record
0
Indexed Sequential Files
An indexed sequential file is like a direct access file with the additional facility of accessing the components in a sequential manner, beginning from the position of the component selected at random. This requires the index to be ordered by key values.