Previous: 13.1 Block Input/Output
Up: 13 Files and the Operating System
Next: 13.3 A Small Data Base Example
Previous Page: 13.1 Block Input/Output
Next Page: 13.3 A Small Data Base Example
The file functions described in the last section, as well as the formatted file I/O functions, read and write data sequentially from the current position in the file. This position in the stream is one of the pieces of information stored in the FILE data structure which we get a pointer to when the file is opened. The standard library provides a function to determine the current position, ftell(). The position returned from ftell() is in terms of the number of bytes offset from the beginning of the file. The library also provides a function which allows a program to move the current position elsewhere in stream: fseek().
Returns: the current file pointer in stream measured in bytes from the beginning. If there is an error, it returns -1L.

in:
Returns: 0 if successful; nonzero on failure. (DOS returns zero even if pointer is not repositioned.)
Description: The function, fseek, sets the file pointer for stream to a new position that is offset bytes from the location specified by base. For binary files, offset is the number of bytes to offset. For text files, offset must be zero or a value returned by ftell(). The value of base must be 0 to indicate the beginning of the file, 1 for the current position, or 2 for the end of file. stdio.h defines three constants for the base position: SEEK_SET is 0, SEEK_CUR is 1, and SEEK_END is 2.
Let us illustrate the use of the above functions in a simple program. Suppose we wish to write a program that opens a file and begins to read items from the file. Suppose at some point, the program requires the size of the file. Then, after the size is determined, the program should resume reading items from where it left off.
The program will: read and print a number of strings from a file;
then, call a
function filesize() that returns the size of the file;
and finally, resume reading and printing strings from the file.
The program driver is shown in Figure 13.3.
Let us now write filesize(),
which returns the size of the file, fp.
It determines the size of the file by moving the current position
to the end of the file
and finding the offset of the pointer in bytes.
However, before the current position is moved to the end of the file,
the function must save the
position where it left off. This saved position is restored by filesize()
before it returns to the calling program.
The function is shown in Figure 13.4.
The function first saves the current position in savepos; then moves the pointer to the end of the file. It uses 0L for offset since the offset must be long. Next, the function uses ftell() to find the offset in bytes of the current position, which is now at the end of the file. The value of end is, therefore, the size of the file. Finally, the saved position is restored and the file size is returned. Here is a sample session:
In the sample session, the first three lines of payin.dat are printed and then the size of the file is printed as 238 bytes. Finally, the rest of the file payin.dat is printed.
A few comments on the use of fseek():