File I/O

Photo by Kari Shea on Unsplash

File I/O

File descriptor system calls, beginner's guide

·

3 min read

I have seen the light so here we go;

In this article, I will be discussing the system calls(functions) relating to file descriptors and the parameters they take.

File descriptor

File descriptor (fd) - It is a non-negative integer that is returned by the operating system when a file or device is opened by an application. It's used to read data from a file, write data to a file, or perform other I/O operations on the file.

Standard I/O streams and their file descriptors

  • Standard input (stdin)/ STDIN_FILENO

it is used to read input from the user (keyboard). Its fd is 0

  • Standard output (stdout)/ STDOUT_FILENO

It is used to write output to the user (screen). Its fd is 1

  • Standard error (stderr)/ STDERR_FILENO

it is used to write error messages or diagnostic output to the user (terminal/screen). Its file descriptor is 2

Open or Create a File

The open() function opens a file and returns the file descriptor that can be used for subsequent I/O operations and returns -1 on error.

prototype;

int open(const char* pathname, int flags); // Open an existing file
int open(const char* pathname, int flags, [mode_t mode]); // Open or create a file if it does not exist

pathname - A pointer to a null-terminated string which is the path to the file you want to open.

flags - An int that specifies the mode in which the file should be opened. O_RDONLY (open for reading), O_WRONLY (open for writing), O_RDWR (open for both reading and writing).

other flags;

O_CREAT opens and creates the file if it does not exist.

O_CREAT | O_EXCL Create the file and fail if the file already exists.

O_APPEND specifies that all write operations should append data to the end of the file instead of overwriting existing data.

mode- An argument that specifies the file permissions to use if the file is being created with the 0_CREATE flag to set the file's initial permissions. These modes S_IRUSR S_IWUSR S_IRGRP S_IWGRP allow the current user and group to read or write the file respectively. It is optional so if you are not creating a file you can ignore it.

Read from a file descriptor

The read() function is used to read bytes from a file or file descriptor into a buffer and returns the number of bytes read, if any. Returns 0 at end of the file and -1 on error.

prototype;

ssize_t read(int fd, void *buffer, size_t size);

fd - integer file descriptor representing the file to read from.

buffer - A pointer to a memory area where the read data will be stored.

size - An integer that specifies the maximum number of bytes to be read. Size corresponds to the size of the memory area indicated in the buffer

Write to a file descriptor

The write() function is used to write data to a file or file descriptor. It returns the number of bytes written or -1 if an error occurs.

prototype;

ssize_t write(int fd, const void *buffer, size_t size);

fd - An integer file descriptor representing the file to write to.

buffer - A pointer to a memory area that contains the data to write.

size - An integer that specifies the number of bytes to write.

Close a file decriptor

Close(), as it sounds, closes the file descriptor.

Parameter;

int close(int fd);

Further reading

I know want to go towards the light, This is the path.

Good luck.