@Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.
Œ

ˆ‰SEC66Št�”›Input/Output Overview

@�”˜Most programs need to do either input (reading data) or output (writing data), or most frequently both, in order to do anything useful. The GNU C library provides such a large selection of input and output functions that the hardest part is often deciding which function is most appropriate!
This chapter introduces concepts and terminology relating to input and output. Other chapters relating to the GNU I/O facilities are: þÂ
• section ‰ŠP�”›Input/Output on Streams@�”˜, which covers the high-level functions that operate on streams, including formatted input and output.
• section ‰ŠP�”›Low-Level Input/Output@�”˜, which covers the basic I/O and control functions on file descriptors.
• section ‰ŠP�”›File System Interface@�”˜, which covers functions for operating on directories and for manipulating file attributes such as access modes and ownership.
• section ‰ŠP�”›Pipes and FIFOs@�”˜, which includes information on the basic interprocess communication facilities.
• section ‰ŠP�”›Sockets@�”˜, which covers a more complicated interprocess communication facility with support for networking.
• section ‰ŠP�”›Low-Level Terminal Interface@�”˜, which covers functions for changing how input and output to terminal or other serial devices are processed.Àð
ˆ‰SEC67Št�”›Input/Output Concepts

@�”˜Before you can read or write the contents of a file, you must establish a connection or communications channel to the file. This process is called Bopening@ the file. You can open a file for reading, writing, or both. ‰IDX414Š
@�”˜The connection to an open file is represented either as a stream or as a file descriptor. You pass this as an argument to the functions that do the actual read or write operations, to tell them which file to operate on. Certain functions expect streams, and others are designed to operate on file descriptors.
When you have finished reading to or writing from the file, you can terminate the connection by Bclosing@ the file. Once you have closed a stream or file descriptor, you cannot do any more input or output operations on it.
ˆ‰SEC68ŠT�”›Streams and File Descriptors

@�”˜When you want to do input or output to a file, you have a choice of two basic mechanisms for representing the connection between your program and the file: file descriptors and streams. File descriptors are represented as objects of type Hint@, while streams are represented as HFILE *@ objects.
File descriptors provide a primitive, low-level interface to input and output operations. Both file descriptors and streams can represent a connection to a device (such as a terminal), or a pipe or socket for communicating with another process, as well as a normal file. But, if you want to do control operations that are specific to a particular kind of device, you must use a file descriptor; there are no facilities to use streams in this way. You must also use file descriptors if your program needs to do input or output in special modes, such as nonblocking (or polled) input (see section ‰ŠP�”›File Status Flags@�”˜).
Streams provide a higher-level interface, layered on top of the primitive file descriptor facilities. The stream interface treats all kinds of files pretty much alike--the sole exception being the three styles of buffering that you can choose (see section ‰ŠP�”›Stream Buffering@�”˜).
The main advantage of using the stream interface is that the set of functions for performing actual input and output operations (as opposed to control operations) on streams is much richer and more powerful than the corresponding facilities for file descriptors. The file descriptor interface provides only simple functions for transferring blocks of characters, but the stream interface also provides powerful formatted input and output functions (Hprintf@ and Hscanf@) as well as functions for character- and line-oriented input and output.
Since streams are implemented in terms of file descriptors, you can extract the file descriptor from a stream and perform low-level operations directly on the file descriptor. You can also initially open a connection as a file descriptor and then make a stream associated with that file descriptor.
In general, you should stick with using streams rather than file descriptors, unless there is some specific operation you want to do that can only be done on a file descriptor. If you are a beginning programmer and aren't sure what functions to use, we suggest that you concentrate on the formatted input functions (see section ‰ŠP�”›Formatted Input@�”˜) and formatted output functions (see section ‰ŠP�”›Formatted Output@�”˜).
If you are concerned about portability of your programs to systems other than GNU, you should also be aware that file descriptors are not as portable as streams. You can expect any system running ISO C to support streams, but non-GNU systems may not support file descriptors at all, or may only implement a subset of the GNU functions that operate on file descriptors. Most of the file descriptor functions in the GNU library are included in the POSIX.1 standard, however.
ˆ‰SEC69ŠT�”›File Position

@�”˜One of the attributes of an open file is its Bfile position@ that keeps track of where in the file the next character is to be read or written. In the GNU system, and all POSIX.1 systems, the file position is simply an integer representing the number of bytes from the beginning of the file.
The file position is normally set to the beginning of the file when it is opened, and each time a character is read or written, the file position is incremented. In other words, access to the file is normally Bsequential@. ‰IDX415Š‰IDX416Š
@�”˜Ordinary files permit read or write operations at any position within the file. Some other kinds of files may also permit this. Files which do permit this are sometimes referred to as Brandom-access@ files. You can change the file position using the Hfseek@ function on a stream (see section ‰ŠP�”›File Positioning@�”˜) or the Hlseek@ function on a file descriptor (see section ‰ŠP�”›Input and Output Primitives@�”˜). If you try to change the file position on a file that doesn't support random access, you get the HESPIPE@ error. ‰IDX417Š
@�”˜Streams and descriptors that are opened for Bappend access@ are treated specially for output: output to such files is Balways@ appended sequentially to the Bend@ of the file, regardless of the file position. However, the file position is still used to control where in the file reading is done. ‰IDX418Š
@�”˜If you think about it, you'll realize that several programs can read a given file at the same time. In order for each program to be able to read the file at its own pace, each program must have its own file pointer, which is not affected by anything the other programs do.
In fact, each opening of a file creates a separate file position. Thus, if you open a file twice even in the same program, you get two streams or descriptors with independent file positions.
By contrast, if you open a descriptor and then duplicate it to get another descriptor, these two descriptors share the same file position: changing the file position of one descriptor will affect the other.
ˆ‰SEC70Št�”›File Names

@�”˜In order to open a connection to a file, or to perform other operations such as deleting a file, you need some way to refer to the file. Nearly all files have names that are strings--even files which are actually devices such as tape drives or terminals. These strings are called Bfile names@. You specify the file name to say which file you want to open or operate on.
This section describes the conventions for file names and how the operating system works with them. ‰IDX419Š
ˆ‰SEC71ŠT�”›Directories

@�”˜In order to understand the syntax of file names, you need to understand how the file system is organized into a hierarchy of directories.
‰IDX420Š‰IDX421Š‰IDX422Š@�”˜A Bdirectory@ is a file that contains information to associate other files with names; these associations are called Blinks@ or Bdirectory entries@. Sometimes, people speak of "files in a directory", but in reality, a directory only contains pointers to files, not the files themselves.
‰IDX423Š@�”˜The name of a file contained in a directory entry is called a Bfile name component@. In general, a file name consists of a sequence of one or more such components, separated by the slash character (H`/'@). A file name which is just one component names a file with respect to its directory. A file name with multiple components names a directory, and then a file in that directory, and so on.
Some other documents, such as the POSIX standard, use the term Bpathname@ for what we call a file name, and either Bfilename@ or Bpathname component@ for what this manual calls a file name component. We don't use this terminology because a "path" is something completely different (a list of directories to search), and we think that "pathname" used for something else will confuse users. We always use "file name" and "file name component" (or sometimes just "component", where the context is obvious) in GNU documentation. Some macros use the POSIX terminology in their names, such as HPATH_MAX@. These macros are defined by the POSIX standard, so we cannot change their names.
You can find more detailed information about operations on directories in section ‰ŠP�”›File System Interface@�”˜.
ˆ‰SEC72ŠT�”›File Name Resolution

@�”˜A file name consists of file name components separated by slash (H`/'@) characters. On the systems that the GNU C library supports, multiple successive H`/'@ characters are equivalent to a single H`/'@ character.
‰IDX424Š@�”˜The process of determining what file a file name refers to is called Bfile name resolution@. This is performed by examining the components that make up a file name in left-to-right order, and locating each successive component in the directory named by the previous component. Of course, each of the files that are referenced as directories must actually exist, be directories instead of regular files, and have the appropriate permissions to be accessible by the process; otherwise the file name resolution fails.
‰IDX425Š‰IDX426Š@�”˜If a file name begins with a H`/'@, the first component in the file name is located in the Broot directory@ of the process (usually all processes on the system have the same root directory). Such a file name is called an Babsolute file name@.
‰IDX427Š@�”˜Otherwise, the first component in the file name is located in the current working directory (see section ‰ŠP�”›Working Directory@�”˜). This kind of file name is called a Brelative file name@.
‰IDX428Š@�”˜The file name components H`.'@ ("dot") and H`..'@ ("dot-dot") have special meanings. Every directory has entries for these file name components. The file name component H`.'@ refers to the directory itself, while the file name component H`..'@ refers to its Bparent directory@ (the directory that contains the link for the directory in question). As a special case, H`..'@ in the root directory refers to the root directory itself, since it has no parent; thus H`/..'@ is the same as H`/'@.
Here are some examples of file names:
H`/a'
Â@The file named H`a'@, in the root directory.
ÀH`/a/b'
Â@The file named H`b'@, in the directory named H`a'@ in the root directory.
ÀH`a'
Â@The file named H`a'@, in the current working directory.
ÀH`/a/./b'
Â@This is the same as H`/a/b'@.
ÀH`./a'
Â@The file named H`a'@, in the current working directory.
ÀH`../a'
Â@The file named H`a'@, in the parent directory of the current working directory.À

A file name that names a directory may optionally end in a H`/'@. You can specify a file name of H`/'@ to refer to the root directory, but the empty string is not a meaningful file name. If you want to refer to the current working directory, use a file name of H`.'@ or H`./'@.
Unlike some other operating systems, the GNU system doesn't have any built-in support for file types (or extensions) or file versions as part of its file name syntax. Many programs and utilities use conventions for file names--for example, files containing C source code usually have names suffixed with H`.c'@---but there is nothing in the file system itself that enforces this kind of convention.
ˆ‰SEC73ŠT�”›File Name Errors

‰IDX429Š‰IDX430Š
@�”˜Functions that accept file name arguments usually detect these Herrno@ error conditions relating to the file name syntax or trouble finding the named file. These errors are referred to throughout this manual as the Busual file name errors@.
HEACCES
Â@The process does not have search permission for a directory component of the file name.
ÀHENAMETOOLONG
Â@This error is used when either the the total length of a file name is greater than HPATH_MAX@, or when an individual file name component has a length greater than HNAME_MAX@. See section ‰ŠP�”›Limits on File System Capacity@�”˜. In the GNU system, there is no imposed limit on overall file name length, but some file systems may place limits on the length of a component.
ÀHENOENT
Â@This error is reported when a file referenced as a directory component in the file name doesn't exist, or when a component is a symbolic link whose target file does not exist. See section ‰ŠP�”›Symbolic Links@�”˜.
ÀHENOTDIR
Â@A file that is referenced as a directory component in the file name exists, but it isn't a directory.
ÀHELOOP
Â@Too many symbolic links were resolved while trying to look up the file name. The system has an arbitrary limit on the number of symbolic links that may be resolved in looking up a single file name, as a primitive way to detect loops. See section ‰ŠP�”›Symbolic Links@�”˜.À

ˆ‰SEC74ŠT�”›Portability of File Names

@�”˜The rules for the syntax of file names discussed in section ‰ŠP�”›File Names@�”˜, are the rules normally used by the GNU system and by other POSIX systems. However, other operating systems may use other conventions.
There are two reasons why it can be important for you to be aware of file name portability issues: þÂ
• If your program makes assumptions about file name syntax, or contains embedded literal file name strings, it is more difficult to get it to run under other operating systems that use different syntax conventions.
• Even if you are not concerned about running your program on machines that run other operating systems, it may still be possible to access files that use different naming conventions. For example, you may be able to access file systems on another computer running a different operating system over a network, or read and write disks in formats used by other operating systems.Àð
The ISO C standard says very little about file name syntax, only that file names are strings. In addition to varying restrictions on the length of file names and what characters can validly appear in a file name, different operating systems use different conventions and syntax for concepts such as structured directories and file types or extensions. Some concepts such as file versions might be supported in some operating systems and not by others.
The POSIX.1 standard allows implementations to put additional restrictions on file name syntax, concerning what characters are permitted in file names and on the length of file name and file name component strings. However, in the GNU system, you do not need to worry about these restrictions; any character except the null character is permitted in a file name string, and there are no limits on the length of file name strings.
Œ
Go to the ‰ŠP�”›first@�”˜, ‰ŠP�”›previous@�”˜, ‰ŠP�”›next@�”˜, ‰ŠP�”›last@�”˜ section, ‰ŠP�”›table of contents@�”˜.