Random File Access

Sequential file access works like a tape recorder — if you remember using one. Data is read from byte 0 through the last byte in the file, one after the other. Random file access can be sequential, but you can also hop around within the file, reading a chunk here, writing a chunk there. The secret has to do with the how the file position indicator is manipulated.

The file position indicator references the location where data is either read from or written to a file.

When a file is first opened, the indicator roosts at the start of the file, byte 0. As you read, the indicator follows along with the bytes read.

For sequential file access, the file position indicator marches from the file’s first byte to the last, whether reading or writing.

For random file access, you can set the position to an exact location. You can access a specific chunk to read or write. The key to making this operation successful is to ensure that you know where such a specific location is. While you could just dive into a file at offset 180, do you know what’s there? Is such an operation useful? (And, no, you cannot move the file position indicator beyond the End-Of-File marker.)

To make random file access work, the data written to the file must be uniform or in a specific format. For example, a file consisting of all integers. Because the size of an integer is consistent, you can set the file position indicator to fetch the 11th integer in the file without having to read the first 10 integers.

A more common example is when working with structures saved to a file.

A structure is defined as a specific size, which remains constant no matter how its members are filled. You can use random file access with structures to create a database where specific records can be written or read without having to plow through the entire file.

To get this process underway, the following code creates a structure array, family[]. This array’s data is displayed and also written to a file. The code reads back in the data as well, to ensure that everything works:

#include <stdio.h>

int main()
{
    struct person {
        char name[32];
        int age;
        float weight;
    };
    struct person family_in[] = {
        { "Dad", 42, 460.1 },
        { "Mom", 40, 120.0 },
        { "Junior", 15, 150.6 },
        { "Missy", 12, 80.2 },
        { "Baby", 1, 33.9 }
    };
    struct person family_out[5];
    int x;
    const char filename[] = "family.data";
    FILE *fp;
    
    /* open/create the file */
    fp = fopen(filename,"w");
    if( fp == NULL)
    {
        fprintf(stderr,"Unable to create %s\n",filename);
        return(1);
    }

    /* write the array and display its contents */
    puts("Writing Data:");
    for(x=0;x<5;x++)
    {
        printf("%s is %d and wieghs %.1f\n",
                family_in[x].name,
                family_in[x].age,
                family_in[x].weight
              );
        fwrite(&family_in[x],sizeof(struct person),1,fp);
    }
    fclose(fp);

    /* open the file to read in the data */
    puts("Reading Data:");
    fp = fopen(filename,"r");
    if( fp == NULL)
    {
        fprintf(stderr,"Unable to access %s\n",filename);
        return(1);
    }

    for(x=0;x<5;x++)
    {
        fread(&family_out[x],sizeof(struct person),1,fp);
        printf("%s is %d and wieghs %.1f\n",
                family_out[x].name,
                family_out[x].age,
                family_out[x].weight
              );
    }
    fclose(fp);

    return(0);
}

This code is long, but it can be broken down into three distinct parts:

Lines 1 through 20 setup the variables, including defining a structure array family_in and filling it with data.

Lines 22 through 41 open a file, family.data, and write each member of the family structure array to that file.

Lines 43 through 61 open the file family.data for reading, saving the structures into the family_out array and displaying the data.

This code uses sequential file access to write and read the data. In next week’s Lesson, I use random file access to pluck specific members saved to the file and update them.

Leave a Reply