0% found this document useful (0 votes)
16 views

File Handling PDF

The document discusses file handling functions in UNIX systems. It describes functions for creating, opening, closing, reading from, and writing to files. These functions include creat() to create files, open() to open existing files, close() to close files, read() to read data from files, and write() to write data to files. It provides details on what each function does, its parameters, return values, and possible error codes.

Uploaded by

Nikita Borshchov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

File Handling PDF

The document discusses file handling functions in UNIX systems. It describes functions for creating, opening, closing, reading from, and writing to files. These functions include creat() to create files, open() to open existing files, close() to close files, read() to read data from files, and write() to write data to files. It provides details on what each function does, its parameters, return values, and possible error codes.

Uploaded by

Nikita Borshchov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

File handling

Contents
File handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Access rights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
System functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Function creat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Function open . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Function close . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Function read . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Function write . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1
File handling
A file is a defined piece of data stored in the system storage. On UNIX systems, almost everything is a file,
including directories, links, and devices. Thanks to this, we can access them in a similar way. Files do not
have a division into name and extension, but this approach can be used.

Access rights
On UNIX systems, access to files and directories is protected by the so-called access rights, which regulate
the rules on which users can use these resources. We have three types of rights:
1. Read right (r).
2. Write right (w).
3. Enforcement right (x).
These rights are determined independently for the user who owns the file, users in the same group as
the owner, and for other users.
Access rights for a specific group can be written in octal number according to the following table:

Number Rights Symbol


0 none ---
1 execute --x
2 write -w-
3 write + execute -wx
4 read r--
5 read + execute r-x
6 read + write rw-
7 read + write + execute rwx

Using three such octal numbers, we can specify access rights for all groups at once, e.g.:
• 640 = rw-r-----
– owner has read and write rights, group has read right;
• 755 = rwxr-xr-x
– owner has all rights, group and others have read and execute rights;
• 777 = rwxrwxrwx
– all groups have all rights assigned.

2
System functions
Function creat

int creat ( const char * pathname, mode_t mode );

The creat function is used to create new files. We specify the path to the file in the first argument, and
its access rights in the second. After the file is created, it is opened in write mode.
If a file with the given name already exists and the calling process has write access, its contents are
deleted.
The function returns:
• descriptor of the created file (if executed correctly),
• value -1 when the function terminates incorrectly.
Possible error codes on error termination:
• EXIST
– a file with the given name already exists and the O_CREAT and O_EXCL flags were used;
• EFAULT
– the given path is outside the available address space;
• EACCESS
– the process did not access the file;
• ENFILES
– the limit of open files in the system has been reached;
• EMPHIL
– the limit of open files in the process has been reached;
• EROFS
– the indicated file is read-only.
Example of use:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])


{
int desc = creat("file.txt", 0640);
printf("Created file with descriptor %d\n", desc);

return 0;
}

Function open

int open ( const char * pathname, int flags [, mode_t mode] );

The open function is used to open files. The first argument is the path to the file, and the second argument
is the file access method. In the optional third argument, we can specify the access rights to the file.
The function returns:
• open file descriptor (when executed correctly),
• value -1 when the function terminates incorrectly.

3
The possible error codes for failing are the same as in the creat function.
Possible access methods are:
• O_RDONLY
– open in read-only mode;
• O_WRONLY
– open in write-only mode;
• O_RDWR
– open in read and write mode.
The accessor can be bitwise OR with one (or more) of the following flags:
• O_CREAT
– create file if file does not exist;
• O_TRUNC
– clear files (available for O_WRONLY and O_RDWR modes);
• O_EXCL
– throws an error if the file already exists and the O_CREAT flag is used;
• O_APPEND
– data is saved at the end of the file.
Example of use:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])


{
int desc = open("file.txt", O_RDONLY);
printf("Opened file with descriptor %d in a read-only mode.\n", desc);

return 0;
}

Function close

int close ( int fd );

The close function is used to close an open file. As an argument, we pass the file descriptor that we
want to close. When a file is closed, an entry in the descriptor table is released and can be reused when
another file is opened.
The function returns:
• value 0 if executed correctly,
• value -1 on wrong execution.
Possible error codes on error termination:
• EBADF
– the given argument is not a valid open file descriptor.

4
Example of use:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])


{
int desc = open("file.txt", O_RDONLY);
printf("Opened file with descriptor %d in a read-only mode.\n", desc);

close(desc);
printf("Closed that file.\n");

return 0;
}

Function read

int read ( inf fd, void * buf, size_t count );

The read function is used to read data from a file. In the first argument, we pass the file descriptor, in
the second, the address of the buffer to which we want to transfer the data read from the file, and in the
third, the number of bytes to be read.
Reading changes the pointer of the current position in the file. After opening the file, this indicator is set
to 0, i.e. to the beginning of the file, and after subsequent operations it moves towards the end of the file
by as many bytes as it was possible to read.
The function returns:
• the actual number of bytes that were read (when done correctly),
• value -1 when the function terminates incorrectly.
Possible error codes on error termination:
• EINTR
– function call was aborted;
• EAGAIN
– non-blocking I/O is selected with O_NONBLOCK, and there is no data available to read imme-
diately;
• ECI
– input/output error;
• EISDIR
– the given descriptor refers to a directory;
• EBADF
– the specified file descriptor is not a valid file descriptor or has not been opened for reading;
• EINVAL
– the given descriptor points to an object unsuitable for reading;
• EFAULT
– the specified buffer address is outside the available address space.

5
Example of use:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])


{
char buf[512];
int desc = open("file.txt", O_RDONLY);
int size = read(desc, buf, 512);
buf[size] = 0; // c-strings need to be null-terminated
printf("%s", buf); // print contents of the file
close(desc); // close that file
return 0;
}

Function write

int write ( inf fd, void * buf, size_t count );

The write function is used to write data to a file. In the first argument we give the file descriptor, in the
second the address of the buffer from which we want to download the data for writing, and in the third the
number of bytes to write. As with the read function, writing changes the pointer to the current position
in the file.
The function returns:
• the actual number of bytes that were written (when done correctly),
• value -1 when the function terminates incorrectly.
Possible error codes on error termination:
• EBADF
– the specified file descriptor is not a valid file descriptor or has not been opened for writing;
• EINVAL
– the given descriptor points to an object not suitable for writing;
• EFAULT
– the specified buffer address is outside the available address space;
• EPIPE
– the specified descriptor is connected to a pipe or socket whose other end is closed;
• EAGAIN
– non-blocking I/O selected (using O_NONBLOCK) and data cannot be written to a pipe or socket
with descriptor fd immediately;
• EINTR
– function call was aborted;
• ENOSPC
– the device containing the file with the descriptor fd has no space for data.

6
Example of use:
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char* argv[])


{
int desc = creat("file.txt", 0640);
write(desc, "This will be written to a file.\n", 32);
close(desc);
return 0;
}

You might also like