(libc.info.gz) Descriptors and Streams
Info Catalog
(libc.info.gz) File Position Primitive
(libc.info.gz) Low-Level I/O
(libc.info.gz) Stream/Descriptor Precautions
13.4 Descriptors and Streams
============================
Given an open file descriptor, you can create a stream for it with the
'fdopen' function. You can get the underlying file descriptor for an
existing stream with the 'fileno' function. These functions are
declared in the header file 'stdio.h'.
-- Function: FILE * fdopen (int FILEDES, const char *OPENTYPE)
Preliminary: | MT-Safe | AS-Unsafe heap lock | AC-Unsafe mem lock |
POSIX Safety Concepts.
The 'fdopen' function returns a new stream for the file descriptor
FILEDES.
The OPENTYPE argument is interpreted in the same way as for the
'fopen' function ( Opening Streams), except that the 'b'
option is not permitted; this is because GNU systems make no
distinction between text and binary files. Also, '"w"' and '"w+"'
do not cause truncation of the file; these have an effect only when
opening a file, and in this case the file has already been opened.
You must make sure that the OPENTYPE argument matches the actual
mode of the open file descriptor.
The return value is the new stream. If the stream cannot be
created (for example, if the modes for the file indicated by the
file descriptor do not permit the access specified by the OPENTYPE
argument), a null pointer is returned instead.
In some other systems, 'fdopen' may fail to detect that the modes
for file descriptor do not permit the access specified by
'opentype'. The GNU C Library always checks for this.
For an example showing the use of the 'fdopen' function, see
Creating a Pipe.
-- Function: int fileno (FILE *STREAM)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | POSIX Safety
Concepts.
This function returns the file descriptor associated with the
stream STREAM. If an error is detected (for example, if the STREAM
is not valid) or if STREAM does not do I/O to a file, 'fileno'
returns -1.
-- Function: int fileno_unlocked (FILE *STREAM)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | POSIX Safety
Concepts.
The 'fileno_unlocked' function is equivalent to the 'fileno'
function except that it does not implicitly lock the stream if the
state is 'FSETLOCKING_INTERNAL'.
This function is a GNU extension.
There are also symbolic constants defined in 'unistd.h' for the file
descriptors belonging to the standard streams 'stdin', 'stdout', and
'stderr'; see Standard Streams.
'STDIN_FILENO'
This macro has value '0', which is the file descriptor for standard
input.
'STDOUT_FILENO'
This macro has value '1', which is the file descriptor for standard
output.
'STDERR_FILENO'
This macro has value '2', which is the file descriptor for standard
error output.
Info Catalog
(libc.info.gz) File Position Primitive
(libc.info.gz) Low-Level I/O
(libc.info.gz) Stream/Descriptor Precautions
automatically generated by
info2html