TABLE 29.2 origin Values That Can Appear in fseek()
The origin value tells C the position from where you want to access the random file next. After you
position the file pointer with fseek(), you can use file input and output functions to write and read
to and from the file. If you position the file pointer at the end of the file (using SEEK_END) and then
write data, new data goes to the end of the file. If you position the file pointer over existing data
(using SEEK_SET and SEEK_CUR) and then write new data, the new data replaces the existing data.
Warning
Use fseek() for random-access files only. Sequential files can be accessed only in
the order of the data.
Table 29.2’s values are in uppercase, which implies that they’re defined somewhere. They’re defined
in stdio.h using #define directives.
The following program opens a file for random-access mode, writes the letters A through Z to the file,
and then rereads those letters backward. The file doesn’t have to be reopened before the reading
begins because of the random-access mode "w+".
Click here to view code image
// Example program #1 from Chapter 29 of Absolute Beginner's Guide
// to C, 3rd Edition
// File Chapter29ex1.c
/* The program opens a file named letters.txt and prints A through Z
into the file
It then loops backward through the file printing each of the letters
from Z to A. */
#include <stdio.h>
#include <stdlib.h>
FILE * fptr;
main()
{
char letter;
int i;
fptr = fopen("C:\\users\\deanwork\\documents\\letters.txt",
"w+");
if (fptr == 0)
{
printf("There was an error while opening the file.\n");