You are on page 1of 2

LOW LEVEL FILE ACCESS

In lower level file handling we will study the basic opening, closing, reading, and
writing a file through a file descriptor which acts as a pointer or cursor to a
file.It is called lower level file handling because it will execute or do
operations on kernel level not even can open a binary file only to read or write
but can open a device also.

1.OPEN:
The open() call is used to open a file to perform read and write operations on
it and it returns a file descriptor, a small, non-negative integer for use in
subsequent system calls i.e. read and write.
syntax:
int open(char *pathname,int flags);
path name – contains the location or name.
flags –
O_RDONLY open for reading only
O_WRONLY open for writing only
O_RDWR open for reading and writing
O_NONBLOCK do not block on open
O_APPEND append on each write
O_CREAT create file if it does not exist
Return Value : It will return a file descriptor value through which we can write or
read into that file.

2.CREATE:
syntax:
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
Thus the file named by pathname is created, unless it already exists. The file is
then opened for writing only, and is truncated to zero length.
Returned value:
If successful, creat() returns a file descriptor for the open file.
If unsuccessful, creat() returns -1 and sets errno to one of the following
values.

3.READ:
When we want to read a certain number of bytes starting from the current
position in a file, we use the read call.
The syntax is:
#include <unistd.h>
ssize_t read(int fd, void* buf, size_tnoct);
The function returns the number of bytes read, 0 for end of file (EOF) and -1 in
case an error occurred.

4.WRITE:
int write(int fildes, char *buf, unsigned nbyte);
On success, the number of bytes written are returned (zero indicates nothing
was written). On error, -1 is returned, and errno is set appropriately.

5.CLOSE:
The function close closes the file descriptor filedescriptor. Closing a file has
the following consequences:
The file descriptor is deallocated.Any record locks owned by the process on
the file are unlocked.
syntax:
int close (int filedescriptor);

6.lseek:
To read the current file position value from a descriptor, use lseek.
Syntax : int lseek (int filedes,off_t offset,int whence);
The lseek function is used to change the file position of the file with
descriptor filedes.
The whence argument specifies how the offset should be interpreted, in the same
way as for the fseek function, and it must be one of the symbolic
constants SEEK_SET, SEEK_CUR, or SEEK_END.

7.stat:
Stat system call is a system call in Linux to check the status of a file such as
to check when the file was accessed.
syntax:
int stat(const char *path, struct stat *buf);

8.fstat:
fstat is a system call that is used to determine information about a file based
on its file descriptor.
syntax:
int fstat(int filedes, struct stat *buf);

9.umask:
set file mode creation mask.
syntax:
mode_t umask(mode_t mask);
This system call always succeeds and the previous value of the mask is returned.

10.dup:
The dup() system call creates a copy of a file descriptor
*It uses the lowest-numbered unused descriptor for the new descriptor.
*If the copy is successfully created, then the original and copy file descriptors
may be used interchangeably.
*They both refer to the same open file description and thus share file offset and
file status flags.
Syntax:
int dup(int oldfd);
oldfd: old file descriptor whose copy is to be created.

11.dup2():
The dup2() system call is similar to dup() but the basic difference between
them is that instead of using the lowest-numbered unused file descriptor, it uses
the descriptor number specified by the user.
Syntax:
int dup2(int oldfd, int newfd); oldfd: old file descriptor
newfd new file descriptor which is used by dup2() to create.
RETURN VALUE:
dup() and dup2() return the new descriptor, or -1 if an error occurred

You might also like