FILE I/O
The functions available for file I/O—open a
file, read a file, write a file, and so on.
Most Unix file I/O can be performed using
only five functions:
• open
• read
• write
• lseek
• close.
File Descriptors
• To the kernel all open files are referred to
by file descriptors.
• A file descriptor is a nonnegative integer.
When we open an existing file or create a
new file, the kernel returns a file descriptor
to the process.
• When we want to read or write a file, we
identify the file with the file descriptor that
was returned by open or creat.
• shells associate file descriptor 0 with the
standard input of a process, file descriptor
1 with the standard output, and file
descriptor 2 with the standard error.
• The numbers 0, 1, and 2 should be
replaced in POSIX.1 applications with the
symbolic constants STDIN_FILENO,
STDOUT_FILENO, and
STDERR_FILENO.
• These are defined in the <unistd.h>
header.
• File descriptors range from 0 through
OPEN_MAX.
• Older versions of Unix had an upper limit
of 19 (allowing a maximum of 20 open files
per process)
• but this was increased to 63 by many
systems.
open Function
• A file is opened or created by calling the
open function.
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open( const char *pathname, int
oflag, ... /* , mode_t mode */ );
• Returns: file descriptor if OK,
-1 on error
• The pathname is the O_RDONLY Open for reading only.
name of the file to
open or create
• There are a O_WRONL Open for writing only.
Y
multitude of options
for this function,
O_RDWR Open for reading and writing.
which are specified by
the oflag argument.
• One and only one of Most implementations define
O_RDONLY as 0,
these three constants O_WRONLY as 1, and
O_RDWR as 2, for
must be specified. compatibility with older
programs.
The following
constants are
optional:
O_APPEND Append to the end of file on each write.
O_CREAT Create the file if it doesn't exist.
O_EXCL Generate an error if O_CREAT is also
specified and the file already exists.
O_TRUNC If the file exists, and if the file is
successfully opened for either write-only
or read-write, truncate its length to 0.
O_NOCTTY If the pathname refers to a terminal
device,do not allocate the device as the
controlling terminal for this process.
O_NONBLOCK this option sets the nonblocking mode for
both the opening of the file and for
subsequent I/Oflag was introduced.
O_SYNC Have each write wait for physical I/O to
complete.
creat Function
• A new file can also be created by
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int creat(const char *pathname, mode_t
mode);
Returns: file descriptor
opened for write-only if OK,
-1 on error
This function is equivalent to
open (pathname, O_WRONLY | O_CREAT |
O_TRUNC, mode);
• In earlier versions of Unix there was no
way to open a file that didn't already
exist.
• Therefore a separate system call, creat
was needed to create new files.
• With the O_CREAT and O_TRUNC
options now provided by open, a
separate creat function is no longer
needed.
close Function
• An open file is closed by
#include <unistd.h>
int close(int filedes);
Returns: 0 if OK, –1 on error
When a process terminates, all open files
are automatically closed by the kernel.
lseek Function
• An open file can be explicitly positioned by
calling lseek.
#include <sys/types.h>
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int
whence);
Returns: new file offset if OK, -1 on error
• The interpretation of the offset depends on
the value of the whence argument.
• If whence is SEEK_SET, the file's offset is
set to offset bytes from the beginning of
the file.
• If whence is SEEK_CUR, the file's offset is
set to its current value plus the offset. The
offset can be positive or negative.
• If whence is SEEK_END, the file's offset is
set to the size of the file plus the offset.
The offset can be positive or negative.
• A successful call to lseek returns the new
file offset, we can seek zero bytes from the
current position to determine the current
offset.
off_t currpos;
currpos = lseek(fd, 0, SEEK_CUR);
Program 3.1 Test if standard input is
capable of seeking.
#include <sys/types.h>
#include "ourhdr.h“
intmain(void){
if(lseek(STDIN_FILENO, 0,
SEEK_CUR== -1)
printf("cannot seek\n");
else
printf("seek OK\n");
exit(0);}
write Function
Data is written to an open file with the write
function.
#include <unistd.h>
ssize_t write(int filedes,
const void *buff, size_t nbytes);
Returns: number of bytes
written if OK, -1 on error
• The return value is usually equal to the
nbytes argument, otherwise an error has
occurred.
• For a regular file, the write starts at the
file's current offset.
• After a successful write, the file's offset is
incremented by the number of bytes
actually written.