C Programming

C Language: O_DONLY, O_WRONGLY, and O_RDWR Flags

While using any functions in the C programming language, there are certain parameters that are mandatory, whereas the others are optional. The optional parameters are the ones that can be used according to the needs of the program. Otherwise, if they are not needed, they can easily be skipped. The flags in the C programming language generally come under the category of optional parameters. Nevertheless, they still hold importance in specifying the actual behavior of a function. In this article, we will be discussing the three different flags that are mainly associated with the files in C, such as O_RDONLY, O_WRONLY, and O_RDWR, which stand for “open as read-only”, “open as write-only”, and “open for both reading and writing” respectively.

Purpose of the O_RDONLY, O_WRONLY, and O_RDWR Flags in the C Programming Language:

We all understand that the files in Linux or even any other operating system, for that matter, have certain permissions associated with them. For example, some files are only readable, some are only executable, some are only writable, and some are combined. Similarly, while opening a file in a C program, you can specify how you want that particular file to be opened. For example, you might only want to open it for reading purposes, writing purpose, or for reading and writing.

This is where the O_RDONLY, O_WRONLY, and O_RDWR flags come into play. These flags are used with the “open()” function of the C programming language to open a specified file. After providing the file path, you can conveniently specify how you want to open that file by using any of these three flags. Now, when you have understood the purpose of these three flags in the C programming language, you should go through the examples discussed below to clarify the usage of these flags in the C programming language.

Usage of the O_RDONLY, O_WRONLY, and O_RDWR Flags in the C Programming Language:

For learning the usage of the O_RDONLY, O_WRONLY, and O_RDWR flags of the C programming language, you need to look at the following three examples. In these examples, we have separately used these three flags, i.e., one flag for each example, so it becomes easier for you to understand their exact purpose.

Example # 1: Using the O_RDONLY Flag in the C Programming Language

This illustration reveals a basic usage of the O_RDONLY flag of the C programming language. We will try to open a sample file for reading only in this example. The sample C code is as follows:

We can use the “O_RDONLY” flag of the C programming language only if we have included the “sys/types.h”, “sys/stat.h”, and “fcntl.h” header files in our C script. In this simple C program, we have defined an integer type variable “fd” that refers to the file descriptor of the file that we want to open as read-only. Then, we have used the “open()” function of the C programming language and have passed to it the path of the desired file followed by the “O_RDONLY” flag indicating that we want to open the file as read-only. Finally, we have printed a confirmation message on the terminal using the “printf” statement.

To compile this simple script, we have used the command shown below:

$ gcc Flags.c –o Flags

Then, we have used the following command to execute this script:

$ ./Flags

The output of this basic C script is shown in the image below, which indicates that the specified file has been opened as read-only.

Example # 2: Using the O_WRONLY Flag in the C Programming Language

This illustration determines a basic usage of the O_WRONLY flag of the C programming language. We will try to open a sample file for writing only in this example. The sample C code is as follows:

We can use the “O_WRONLY” flag of the C programming language only if we have included the “sys/types.h”, “sys/stat.h”, and “fcntl.h” header files in our C script. In this simple C program, we have defined an integer type variable “fd” that refers to the file descriptor of the file that we want to open as write-only. Then, we have used the “open()” function of the C programming language and have passed to it the path of the desired file followed by the “O_WRONLY” flag indicating that we want to open the file as write-only. Finally, we have printed a confirmation message on the terminal using the “printf” statement.

The output of this basic C script is shown in the image below which indicates that the specified file has been opened as write-only:

Example # 3: Using the O_RDWR Flag in the C Programming Language

This example demonstrates a basic usage of the O_RDWR flag of the C programming language. We will try to open a sample file for both reading and writing in this example. The sample C code is as follows:

We can use the “O_RDWR” flag of the C programming language only if we have included the “sys/types.h”, “sys/stat.h”, and “fcntl.h” header files in our C script. In this simple C program, we have defined an integer type variable “fd” that refers to the file descriptor of the file that we want to open as both readable and writable. Then, we have used the “open()” function of the C programming language and have passed to it the path of the desired file followed by the “O_RDWR” flag, indicating that we want to open the file as both readable and writable. Finally, we have printed a confirmation message on the terminal using the “printf” statement.

The output of this basic C script is shown in the following image which indicates that the specified file has been opened as both readable and writable:

Conclusion:

In this article, we wanted to talk about the three flags associated with the files in Linux, i.e., O_RDONLY, O_WRONLY, and O_RDWR. In this regard, we first explained the purpose of using these flags in the C programming language. After doing that, we walked you through three different examples that use one of these flags. These examples were implemented in the C programming language. After going through these examples, you would have easily understood how to use these flags while opening your files in the C programming language. However, depending upon the access permissions of the target file, you might encounter some errors if you try to open an inaccessible file. Other than that, after going through all three of these examples, you will surely get the proficiency to use these flags while opening your desired files within your C programs on Linux. We hope you found this article helpful. Check out other Linux Hint articles for more tips and tutorials.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.

Page was generated in 0.048818111419678