C Programming

Open 2 C Function

You may have tried many types of C functions while executing the C codes in the Linux platform. These functions can be doing some input and output operations as most functions usually do. One of those 2 C functions is the Open() function. The Open() function in the C programming language opens a file in the specified path or directory. If the specified file indicated in the code does not exist at the specific location, this function may throw an exception or may create it on the specified location/path if certain flags are passed. We can conclude that the open function is valuable equally for reading and writing. So, we cover the use of the Open 2 C function within our Ubuntu 20.04 platform along with some examples.

Syntax
The syntax of the Open() function in the C language is given below. Let’s discuss its parameters:

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

Path

Path is the title of the file that you would like to open or create. It also refers to the file’s location. If we are not working in the same directory as the file, we can provide an absolute path that begins with “/”. We can alternatively specify a relative path where, in some cases, we just mention the file name and extension.

Flags

To utilize the flags, here is the list with their respective explanations:

  • O_RDONLY: In read-only mode, open the file.
  • O_WRONLY: In a write-only mode, open the file
  • O_RDWR: Open the file in reading and write mode
  • O_CREAT: This flag is applied to create a file if it doesn’t exist in the specified path or directory
  • O_EXCL: Prevents the file creation if it already exists in the directory or location.

Here, O stands for Open function.

Header File/Library

The following library or header file is used in the code for this function usage.

#include <fcntl.h>

To create or open a file in that certain directory or path, use the VIM Editor. The “openFile.c” is the name of the file that we created. When we type this command, the editor opens the file in editing mode, permitting us to type the lines of code in the file. To close the VIM editor and save the file, press the escape key, type a colon (:) and x, then press the enter key.

The following lines of code are typed into the “openFile.c” file. We use a relative path to open the “testopen.txt” file in the following code. The O_RDONLY (read only) and O_CREAT flags were passed (create the “testopen.txt” file if it does not exist in the current directory).

The printf function is now used to display the return value in the file descriptor. We then verify if the file descriptor is equal to -1, which indicates that the open file failed and prints the error.

We make use of the GCC compiler to assemble the file. If you don’t have the GCC C-Compiler installed, run the following commands to get it. Simply execute the subsequent instruction in the terminal to see the GCC Compiler version on your Linux-Ubuntu system:

sudo apt update
sudo apt install build-essential

Type the following command to compile the “openFile.c” in the GCC Compiler. The following command includes the GCC compiler. Next, specify the file that we want to compile along with the extension and the -o flag (used to output the file to particular object file which is specified right after this flag):

gcc openFile.c –o openFile.out

Alternatively, we can run the command before the –o flag, which produces an “a.out” object file in the current directory by default. Using the list directory command, check the output or object file, i.e. openFile.out.

Type the following command to execute or run the output or object file, which displays the file descriptor equal to 3. It indicates that the provided file (testopen.txt) is present in the directory containing the output file.

Open the C file with the VIM Editor once more, but this time, modify the file name (openFile1.txt) in the open function. Then, save and close the “openFile.c” file.

Another change in the open command is passing the O_RDONLY flag which opens the “openFile1.txt” in read-only mode. It means that we can only read the data of the file. We cannot perform the write or update function in that specified file.

Compile the file again to update the output file. After that, run the code using the object file. Since we do not have the specified text file in the current directory, the use of the open() function has thrown an error and returns a -1 which is stored in the fileDescriptor variable of the integer type. The following screen displays the output of the openFile. If the output file has not been specified, simply type “./a.out” in the terminal to see the file’s output.

We opened the “openFile.c” file in the VIM editor once more and used the O_EXCL flag in the open command. It implies that if the specified file does not exist in the directory, do not create it; if it does, simply open it. Because there is no “openFile1.txt” file in the list directory, the open method returns an error.

The following screen demonstrates that we do not have the given file in the path, and the open function returns -1. This indicates that no such file or directory exists. If the command for the output file is typed incorrectly, it returns the generic error – “no such file or directory”.

Conclusion

This article is about the use of the Open 2 C function in the Kali Linux system. Using this system call, we discussed how it can be used to open and read the file and its contents easily. We discussed how it throws an error when the file descriptor doesn’t find the required file.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.