Node:Opening and Closing Files, Next:I/O Primitives, Up:Low-Level I/O
This section describes the primitives for opening and closing files
using file descriptors. The open
and creat
functions are
declared in the header file fcntl.h
, while close
is
declared in unistd.h
.
int open (const char *filename, int flags[, mode_t mode]) | Function |
The open function creates and returns a new file descriptor
for the file named by filename. Initially, the file position
indicator for the file is at the beginning of the file. The argument
mode is used only when a file is created, but it doesn't hurt
to supply the argument in any case.
The flags argument controls how the file is to be opened. This is
a bit mask; you create the value by the bitwise OR of the appropriate
parameters (using the The normal return value from
If on a 32 bit machine the sources are translated with
This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time The |
int open64 (const char *filename, int flags[, mode_t mode]) | Function |
This function is similar to open . It returns a file descriptor
which can be used to access the file named by filename. The only
difference is that on 32 bit systems the file is opened in the
large file mode. I.e., file length and file offsets can exceed 31 bits.
When the sources are translated with |
int creat (const char *filename, mode_t mode) | Obsolete function |
This function is obsolete. The call:
creat (filename, mode) is equivalent to:
open (filename, O_WRONLY | O_CREAT | O_TRUNC, mode) If on a 32 bit machine the sources are translated with
|
int creat64 (const char *filename, mode_t mode) | Obsolete function |
This function is similar to creat . It returns a file descriptor
which can be used to access the file named by filename. The only
the difference is that on 32 bit systems the file is opened in the
large file mode. I.e., file length and file offsets can exceed 31 bits.
To use this file descriptor one must not use the normal operations but
instead the counterparts named When the sources are translated with |
int close (int filedes) | Function |
The function close closes the file descriptor filedes.
Closing a file has the following consequences:
This function is a cancellation point in multi-threaded programs. This
is a problem if the thread allocates some resources (like memory, file
descriptors, semaphores or whatever) at the time The normal return value from
Please note that there is no separate |
To close a stream, call fclose
(see Closing Streams) instead
of trying to close its underlying file descriptor with close
.
This flushes any buffered output and updates the stream object to
indicate that it is closed.