This blog is under construction

Sunday, 20 May 2012

Sequential and random file access in C

There are two types of files.  They are
1. Data file(Eg. record.dat)
2. Text file(Eg. info.txt)

Data file:
Data file can be used to store information in the form of records.  For example, consider a structure with 3 data members(rollno, name and age).  We can store structure objects in the form of records in data file as shown below.

+-------------------------------+
| 101      Tom           10     |  Record 1
+-------------------------------+
| 102      Raj             10     |  Record 2
+-------------------------------+
| 103      Taj             11     |  Record 3
+-------------------------------+

Text file:
Text file is used to store information in the form of characters as shown below.

jp@jp-VirtualBox:~/$ cat input.txt 
one Hello world 12 dsf
one Hello world 123
Hello worldad a

Here, input.txt is a text file.

There are two type of file access.  They are
1. Sequential file access
2. Random file access

Sequential file access:
Data are stored one after another(sequentially) in the case of text files.  So, data are accessed in the same way in which they are stored.

Example C program to illustrate sequential file access

  #include <stdio.h>
  int main() {
        char ch;
        FILE *fp;
        fp = fopen("input.txt", "r");

        /*
         * print the content of input.txt
         * on the output screen
         */
        ch = feof(fp);
        while (!feof(fp)) {
                printf("%c", ch);
                ch = fgetc(fp);
        }
        fclose(fp);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ cat input.txt 
  one Hello world 12 dsf
  one Hello world 123
  Hello worldad a

  jp@jp-VirtualBox:~/$ ./a.out
  one Hello world 12 dsf
  one Hello world 123
  Hello worldad a



Random file access:
Data can be read or modified randomly in data file, since the information are stored in the form of records. By moving file pointer, we can access any needed information from a file.  To read last record of a file, we don't need to read through the entire file.  Instead, we can move the file pointer to last record and access it.  Random file access is more efficient when compared to sequential file access.

Example C program to illustrate random file access

  #include <stdio.h>
  #include <string.h>
  #define NO_OF_RECORDS 5
  struct student {
        int age;
        char name[32];
        int rollno;
  };

  int main() {
        int i = 0, n;
        FILE *fp;
        struct student obj;
        fp = fopen("input.dat", "w");

        /* get five records(age, name, rollno) from user */
        while (i < NO_OF_RECORDS) {
                fscanf(stdin, "%d %s %d", &obj.age, obj.name, &obj.rollno);
                fwrite(&obj, sizeof(obj), 1, fp);
                getchar();
                i++;
        }
        fclose(fp);
        fp = fopen("input.dat", "r");
        /* access the needed record by moving file pointer */
        printf("Enter your needed record(1-5):");
        scanf("%d", &i);

        /* moving file pointer to access corresponding record */
        fseek(fp, (i-1) * sizeof(obj), 0);

        /* read the record data */
        fread(&obj, sizeof(obj), 1, fp);

        /* print the output */
        printf("age: %d\n", obj.age);
        printf("Name: %s\n", obj.name);
        printf("rollno: %d\n", obj.rollno);
        fclose(fp);
        return 0;
  }

  Output:
  jp@jp-VirtualBox:~/$ ls input.dat
  ls: cannot access input.dat: No such file or directory
  jp@jp-VirtualBox:~/$ ./a.out
  101 Tom 12
  102 Raja 13
  103 Mike 14
  105 Julie 13
  106 Fred 12
  Enter your needed record(1-5):1
  age: 101
  Name: Tom
  rollno: 12
  jp@jp-VirtualBox:~/$ cat input.dat 
  eTomه$C(�?(��8K׿�� �{�
  jFred$C(�?(��8K׿�� �{��iJulie$C(�?(��8K׿�� �{�


The above program inputs student details from the user and writes the same in a binary file(input.dat) in the form of records.  User can access any record randomly by providing the record number as input.  Data files are not in human readable format which can be inferred from the cat output of input.dat file.


1 comment:

  1. Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324

    ReplyDelete