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 9999 th 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.
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
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.
1 #include<stdio.h>
struct emp
2
{
3 int empno;
4 char ename[20];
5 int sal;
6 long int phno;
};
7 int main()
8 {
9 struct emp x;
10 FILE *p;
11 int n,i,t,rec;
char choice[1];
12 p=fopen("employee","wb+"); /* opening the file in binary write/read mode*/
13 printf("How many records?");
14 scanf("%d",&n);
15 printf("Enter %d records:\n",n);
for(i=1;i<=n;i++)
16 {
17 printf("\n\nEmpno:");
18 scanf("%d",&x.empno);
19 printf("Name:");
20 scanf("%s",x.ename);
printf("Salary:"); /* Accepting the data from keyboard*/
21
22
23
24
25
26 scanf("%d",&x.sal);
printf("Phone No:");
27 scanf("%ld",&x.phno);
28 fwrite(&x,sizeof(x),1,p); /* Writing objects onto the file*/
29 }
30 printf("Press any key to continue....\n");
getchar(); /* To wait for user confirmation*/
31 while(1)
32 {
33 printf("\nEnter the record number:");
34 scanf("%d",&rec); /* Accepting record number */
35 if(rec<1||rec>n) /* Checking for validity of record numb
printf("Search failure...");
36 else
37 {
38 fseek(p,(rec-1)*sizeof(x),0); /* placing record pointer to the required record*/
39 fread(&x,sizeof(x),1,p); /* Reading the record*/
40 printf("Empno:%d\nName:%s\nSalary:%d\nPhno:%ld",x.empno,x.ename,x.sal,x.phno); /*
41 Printing the record*/
}
42 printf("\nWant to continue...y/n:"); /* accepting confirmation to continue*/
43 scanf("%s",choice);
44 if(strcmp(choice,"n")==0)
45 break;
}
46
fclose(p);
47 return 0;
48 }
49
50
51
52
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