-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path03-random_access.c
50 lines (39 loc) · 1.15 KB
/
03-random_access.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
// Reads data in the reverse order
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
long curr_pos; // for ftell()
if ((fptr = fopen("program.bin","rb")) == NULL){
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
// Moves the cursor to the end of the file
fseek(fptr, -sizeof(struct threeNum), SEEK_END);
curr_pos = ftell(fptr);
printf("\nCurrent position in file (bytes): %ld", curr_pos);
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("\nn1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3);
fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR);
curr_pos = ftell(fptr);
printf("\nCurrent position in file (bytes): %ld", curr_pos);
}
fseek(fptr, -sizeof(struct threeNum), SEEK_END);
curr_pos = ftell(fptr);
printf("\nCurrent position in file (bytes): %ld", curr_pos);
rewind(fptr);
curr_pos = ftell(fptr);
printf("\nCurrent position in file after rewind (bytes): %ld\n", curr_pos);
fclose(fptr);
return 0;
}