Objective:: System Calls Related To File Manipulation
Objective:: System Calls Related To File Manipulation
Objective:: System Calls Related To File Manipulation
Objective:
Practicing simple programs using some simple file structure related
system calls to get an overview of system calls.
creat( )
The prototype for the creat( ) system call is:
int creat(file_name, mode)
char *file_name;
int mode;
where file_ name is pointer to a null terminated character string that names the file and mode
defines the file's access permissions. The mode is usually specified as an octal number such as
0666 that would mean read/write permission for owner, group, and others or the mode may also
be entered using manifest constants defined in the "/usr/include/sys/stat.h" file. If the file
named by file_name does not exist, the UNIX system creates it with the specified mode
permissions. However, if the file does exist, its contents are discarded and the mode value is
ignored. The permissions of the existing file are retained. Following is an example of how to use
creat( ):
/* This program explains use of creat( )
system call*/
#include <stdio.h>
#include <sys/types.h> /* defines types */
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */
int main( )
{
int fd;
fd = creat("datafile.dat", S_IREAD | S_IWRITE);
if (fd == -1)
printf("Error in opening datafile.dat\n");
else
{
printf("datafile.dat opened for read/write
access\n");
printf("datafile.dat is currently empty\n");
}
close(fd);
exit (0);
}
open( )
The file management system calls allow you to manipulate the full collection of regular,
directory, and special files. In most cases, open ( ) is used to initially access or create a file. If the
file system call succeeds, it returns a small integer called a file descriptor that is used in
subsequent I/O operations on that file. If open ( ) fails, it returns 1. When a process no longer
needs to access an open file, it should close it using the close ( ) system call. All of a processs
open files are automatically closed when the process terminates. Although this means that you
may often omit an explicit call to close ( ), its better programming practice to explicitly close
your files.
File descriptors are numbered sequentially, starting from zero. Every process may have up to
20 (the usual system limit) open files at any one time, with the file descriptor values ranging
between 0 and 19. By convention the first three file descriptor values have special meaning:
Value
0
1
2
Meaning
For example, the printf ( ) library function always sends its output using file descriptor 1, and
scanf ( ) always reads its input using file descriptor 0 which is the display screen. When a
reference to a file is closed, the file descriptor is freed and may be reassigned by a subsequent
open ( ).
A single file may be opened several times and thus have several file descriptors
associated with it.
Each file descriptor has its own private set of properties that have nothing to do with the file.
When a file descriptor is created, its file pointer is positioned at offset 0 in the file (the first
character) by default. As the process reads and/or writes, the file pointer is updated
accordingly. For example, if a process opened a file and then read 10 bytes from the file, the
file pointer would end up positioned at offset 10. If the process then wrote 20 bytes, the bytes at
offset 10..29 in the file would be overwritten and the file pointer would end up positioned at
offset 30.
Meaning
Open for reading only
Open for writing only
Open for reading and writing
Used wih communications devices
Used with communication devioces
Open at end of file for writing
If set this results in write() calls not returning until data has actually been
written to dics.
Create the file if it doesn't already exist
If set and O_CREAT set will cause open() to fail if the file already exists
Truncate file size to zero if it already exists
where file_name is a pointer to the character string that names the file, option_flags represent
the type of channel, and mode defines the file's access permissions if the file is being created.
Multiple values are combined using the | operator (i.e. bitwise OR). Note: some combinations are
mutually exclusive such as: O_RDONLY | O_WRONLY and will cause open( ) to fail. If the
O_CREAT flag is used, then a mode argument is required. The mode argument may be
specified in the same manner as in the creat( ) system call.
close( )
int close (int fd)
close ( ) frees the file descriptor fd. If fd is the last file descriptor associated with a particular
open file, the resources associated with that file are deallocated.
If successful close ( ) returns zero, otherwise it returns 1.
To close a channel, use the close( ) system call. The prototype for the close( ) system call is:
int close(file_descriptor)
int file_descriptor;
where file_descriptor identifies a currently open channel. close( ) fails if file_descriptor does
not identify a currently open channel.
read( )
int read (int fd, char *buf, int count)
read ( ) copies count bytes from the file referenced by the file descriptor fd into the buffer
(character array) buf. The bytes are read from the current file position, which is then
updated accordingly. read ( ) copies as many bytes from the file as it can, up to the number
specified by count, and returns the number of bytes actually copied. If a read ( ) is attempted
after the last byte has already been read, it returns 0, which indicates end-of-file.
If successful, read ( ) returns the number of bytes that it read; otherwise, it returns 1.
write( )
int write (int fd, char *buf, int count)
write ( ) copies count bytes from a buffer (character array) buf to the file referenced by the file
descriptor fd. The bytes are written at the current file position, which is then updated
accordingly. If the O_APPEND flag was set for fd, the file position is set to the end of the file
before each write. write ( ) copies as many bytes from the buffer as it can, up to the number
specified by count , and returns the number of bytes actually copied. If the returned value is
not count, then the disk probably filled up and no space was left.
If successful, write ( ) returns the number of bytes that it wrote; otherwise, it returns 1.
The read( ) system call does all input and the write( ) system call does all output. When used
together, they provide all the tools necessary to do input and output sequentially.
Both read( ) and write( ) take three arguments. Their prototypes are:
int read(file_descriptor, buffer_pointer, transfer_size)
int file_descriptor;
char *buffer_pointer;
unsigned transfer_size;
*/
fd = open("datafile.dat",O_RDWR | O_CREAT |
O_EXCL, S_IREAD | S_IWRITE);
if (fd != -1)
{
printf("datafile.dat opened for read/write
access\n");
write(fd, message,
sizeof(message)); close (fd);
fd = open("datafile.dat",O_RDWR | O_CREAT |
O_EXCL, S_IREAD | S_IWRITE);
if (read(fd, buffer, sizeof(message))
== sizeof(message))
printf("\"%s\" was written to datafile.dat\n",
buffer);
else
printf("*** error reading datafile.dat ***\n");
close (fd);
}
else
printf("*** datafile.dat already exists ***\n");
exit (0);
}
lseek()
long lseek (int fd, long offset, int where)
lseek ( ) allows you to change a descriptors current file position. fd is the file descriptor,
offset is a long integer, and where describes how offset should be interpreted. The three
possible values of where are defined in /usr/include/sys/file.h, and have the
VALUE
SEEK_SET
SEEK_CUR
SEEK_END
MEANING
offset is relative to the start of the file.
offset is relative to the current file position.
offset is relative to the end of the file.