Working with Files Under UNIX

A file or device under UNIX is opened with the open(2) system call. Before open(2) is considered in detail, let's first examine the way UNIX references open files in general.

When you want to read from a file, such as /etc/hosts, you must indicate which file you want to read. However, if you had to name the path as a C string "/etc/hosts" each time you wanted to read part of the file, this would not only be tedious and inefficient, it would also be inflexible. How would you read from different parts of the same file? Obviously, a method by which the file can be opened more than once is much more flexible.

When you open a file under UNIX, you are given a reference to that file. You already know (since this is ...

Get Advanced UNIX Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.