How to open a file in C
C is a general-purpose programming language that is known for its efficiency and ability to perform low-level operations that involve communicating with the underlying operating system kernel, such as file manipulation operations.
Below, we can see the two different functions used to open files in C programs.
The
fopen()function, provided bystdio.hlibrary.The
open()function, provided byfcntl.hlibrary.
In this Answer, we will discuss how both of the functions mentioned above are used to open files.
The fopen() function
This function is provided by the standard C library, stdio.h. It is used to open a file and perform various file manipulation functions.
If a file exists, only then will the function open it else, it will create a new file.
Syntax
Below, we can see a code snippet displaying the syntax of the function.
FILE *fopen(const char *fileName, const char *modeToOpen);
We use the fopen() function to open a file, which returns a pointer to the file of type FILE. If there was any error opening the file, the function returns NULL.
In the function, we pass two arguments, one of which is a string fileName that represents the file's name, and the other argument is the modeToOpen which specifies in which mode we are to open the file.
In the table below, we can see the most frequently used modes of opening a file using the fopen() function.
Modes
Modes | Description |
r | Read data from a file |
w | Write data to the file |
a | Append data at the end of file |
r+ | For both read and write, file must exist on the system |
w+ | Create a new file and open in read and write mode |
a+ | Create a new file and open in append mode. |
Code example
Below, we can see a code sample attached that shows how to use the fopen() function using the three most used modes, r, w and a.
Note: The empty text file
sample.txthas already been uploaded, and is placed in the same folder as the application's code.
Code explanation
Below we can see an explanation of the code attached above.
Line 5: We will first create a file pointer to store the pointer to the file.
Lines 8–12: Now we will open the file
sample.txtin write mode and write the string "Hello Educative User!" into the file. Once done, close the file using thefclose()function.Lines 15–19: To add data to the existing file
sample.txtwe will now open it in append mode and append the string How are you? to the file.Lines 22–29: Finally, we open the file in read mode and read the contents of the file using the
fgetc()function in a while loop.
The open() function
This function is a low-level system call that is used for opening files. This function can only be used while we running the program on a Linux environment.
To use this method, we will have to include two libraries, unistd.h to run system calls and fcntl.h for file control options.
Syntax
Below, we can see a code snippet displaying the syntax of the function.
int open(const char *fileName, openmode modeToOpen);
The function takes two arguments, fileName that is, a string that represents the name of the file and modeToOpen that represents the mode in which we want to open the file.
The function returns an integer variable that represents the file descriptor and performs manipulation operations on the file.
In the table below, we can see the different modes to open a file using the open() function.
Modes
Mode | Description |
O_RDONLY | Open in read only mode |
O_WRONLY | Open in write only mode |
O_RDWR | Open in read and write mode |
O_CREAT | Create a new file |
O_TRUNC | Truncate the file if it exists |
O_APPEND | Open to append data to end of file |
O_EXCL | Used with O_CREAT to ensure file is newly created |
Code example
Below, we can see a code sample attached that shows how to use the open() function to open a file named sample.txt.
Code explanation
Below, we can see an explanation of the code attached above.
Line 8: We create an integer variable to store the opened file descriptor.
Line 11: We open the file using the
open()function. We pass the modesO_CREAT | O_TRUNC| O_WRONLY, meaning the file should be created if it doesn't exist. If it exists, we truncate it and open it in write-only mode. We also give the created new file all permissions specified by the 3rd argument0777.Lines 12–14: In the file, we write Hello Educative User! and then close the file.
Lines 18–22: Now, we open the same file in append mode and append the string How are you?. We then again close the file after we are done writing.
Lines 24–31: We now open the file in read only mode and read the chracters from the file and display them onto the console using the
read()function.
Practice quiz
Now that we have understood how to open a file using the fopen() and the open() function in C, let's try to solve the quiz below to test how much we have learned.
What is the return type of the open() function?
FILE*
char
int
void
Free Resources