- Open the random file in read-only mode using the following code:
fp = fopen (argv[1], "rb");
- If the file does not exist or does not have enough permissions, an error message will be displayed and the program will terminate, as shown in the following code:
if (fp == NULL) { perror ("An error occurred in opening the file\n"); exit(1); }
- To read the random file in reverse order, execute a loop equal to the number of lines in the file. Every iteration of the loop will read one line beginning from the bottom of the file. The following formula will be used to find out the number of lines in the file:
total number of bytes used in the file/size of one line in bytes
The code for doing this is as follows:
fseek(fp, 0L, SEEK_END); ...