next up previous contents Back to Operating Systems Home Page
Next: Reading & Writing Up: Block-oriented I/O in UNIX Previous: Opening files

Moving inside a file

A seek pointer is associated to each open file, indicating the point in the file (i.e. in its byte sequence) where the next read or write operation will take place. The system call lseek(), declared as

off_t lseek (int fildes, off_t offset, int whence);
can be used to enquire about or set this position, for the open file whose file descriptor is fildes. If whence is SEEK_SET, the pointer is set to offset bytes from the beginning of the file. If whence is SEEK_CUR, the pointer is set to its current location plus offset. If whence is SEEK_END, the pointer is set to the size of the file plus offset. The type off_t is usually a synonimus for long int, but this is implementation-dependent. Note that offset cannot be negative for regular files.

On success, lseek() returns the resulting pointer location, as measured in bytes from the beginning of the file, hence to enquire about the current position you can just call it as lseek(fildes, 0, SEEK_CUR). In the BSD flavors of UNIX, system call named tell() is provided for the same purpose: just pass the file descriptor to it, and get the current position on return. As usual, both routines return -1 in case of error, setting errno appropriately.

You are allowed to set the file pointer at a position way beyond the current end of the file. For example,

...
fildes=open("myfile", O_WRONLY|O_TRUNC|O_CREAT, 0644);
if (fildes==-1)
{
    perror("myfile");
    exit(1);
}

lseek(fildes, 100000, SEEK_END);
...
opens a file for writing, discarding its content if it already existed, then sets about to write after a ``hole'' 100,000 bytes long. This doesn't cause UNIX to waste 100,000 bytes of disk space: holes in files created by seeks like the above consume very little storage. If you later attempt to read from within a hole, the system will supply ASCII NULL characters (i.e. zeros), but these character will not be physically on the disk.

Note that not all devices on which a file can logically be stored are capable of seeking. UNIX carefully separates between the concept of file, which is an abstraction attached to a sequence of bytes, and the pysical manifestation of that sequence. As we'll see later, in UNIX a ``file'' can also be, for example, a stream of characters coming from a serial communication line: in this case there's physically no way to backtrack.


next up previous contents Back to Operating Systems Home Page
Next: Reading & Writing Up: Block-oriented I/O in UNIX Previous: Opening files

Franco Callari