Working Directly with System Calls
Explore how to directly work with system calls such as open read and write to perform unbuffered input/output in C. Understand how to properly check return values and handle errors. Practice combining these calls to create a file copy program that manages resources effectively and handles errors gracefully.
We'll cover the following...
Previously, we learned that file descriptors are integers used by the operating system to represent open files and other resources. We now move from understanding descriptors to using them directly. In this lesson, we explore the core system calls and learn how to check return values, handle errors properly, and perform unbuffered I/O for precise control over data transfer.
Opening a file with open()
The open() system call is declared in <fcntl.h> and is used to request access to a file from the operating system. Below is the basic syntax:
int open(const char *pathname, int flags, mode_t mode);
Let's see in practice below:
To see the content in the data.txt file, type the cat data.txt command in the terminal above. In the above code:
Line 6:
open()requests the kernel to open (or create)data.txtwith these flags:O_WRONLY→ open for writingO_CREAT→ create the file if it does not exist...