C Programming

How Do You Write to a File in C?

You must have heard about file handling while working in the C language. This is a concept widely known among C developers and programmers. It is a simple and easy concept of creating a file, opening a file, reading and writing into the file, and closing it. The C language uses different built-in methods to achieve all these functionalities.

Today, we will learn about writing to a file in C language with different built-in functions of file handling while working on the Ubuntu 20.04 Linux system. Start the Linux terminal first using the “Ctrl+Alt+T” shortcut.

Example 01: Using fprintf() Method

We will start our first example by creating a new C file in the Linux system. Thus, the “touch” query will be used for that. The filename has been given as “file.c”. After the creation of a file, we need to open it to write some C code in it. This requires some editor to be installed on your system. There are several editors available in Linux. We are using the “GNU Nano” editor here. So, the “nano” keyword in the command shows the usage of Nano editor to edit the C file.

We have to add the standard libraries of the C language. The very well-known and always required library is a standard input-output header which is added in our code with the “#include” keyword at the top of a file. Start the initialization of a “main” method. Within the main() method, you need to first create a file descriptor i.e. file object. This descriptor will be used to open, close, read, write the file and must be a pointer type.

We have used this file object descriptor to open the file with the use of the “open()” method widely known in the concept of filing in C language. This method takes two arguments in its parameters. The first one is “path” to the file and the other is “mode” in which the file will be opened. Point to be noted that within the Linux environment, you don’t need to create a new file to add data into it.

In Linux, you can simply put up the name within the path parameter of the “open()” method and your file will be automatically generated at the specified location. We have added “w+” mode to let the user write and edit the file.

Now, the main point came. The “fprintf” clause has been used here to write data in the file “new.txt” with the help of its descriptor “f”. After writing in the file, you need to close the file descriptor to quit letting a user do more, with the usage of the “close()” function here. The main method will be ended here. Save this code and quit the editor by utilizing “Ctrl+S” and “Ctrl+X”.

To execute the C code, we need to compile it first. If you don’t have a C compiler in your Linux system, try to get “gcc”. So, we have used the “gcc” command along with the name of a file “file.c” to compile the code within it. If the compilation doesn’t return anything, this means your code is error-free.

We have executed it with the “./a.out” command. The execution also didn’t return anything. Now, check the file that has been created by this code i.e. “new.txt” with the “cat” query. You will see that the data is successfully written into the file.

Example 02: Using fputc() Method

This function is known for writing a single character within the file at a time. Let’s begin to see how it works. Open the same C file to make it up to date. After adding the header input-output library, initialize the main() method. Within the main method, add the pointer type file descriptor. This file descriptor is opening the file “new.txt” with the “fopen()” function of C.

The syntax is the same as per the above example. The only required to change is the “fputc” function here is taking two arguments. One is the data i.e., character and the other is the file descriptor to specify the file. After adding the data into a file, the descriptor is used to close the file with the “fclose()” method.

Compile the file first and execute it after that. Nothing happens because the code was correct. Display the data of a “new.txt” file in the shell with the use of the “cat” command. The old data has been replaced by the character “A”.

Example 03: Using fputs() Method

Within this example, we will be using another function i.e. “fputs” to write data into a text file. The fputs() function differs from the “fputc()” function because it takes string-type values instead of a character. There is no need to alter the overall code. The only change is required at the “fputs()” function line. Replace “fputc” with “fputs”.

We have to change the data that will be written into the file as well. Within both fputc() and fputs() methods, the argument descriptor pointer is used after the “string” while in the “fprintf” it is used first. Save your file to get it updated.

Let’s compile and execute the code once again with “gcc” and “a.out” commands. We got successful as the data is successfully written into the file “new.txt”.

Example 04:

Let’s have a little enhanced example. After opening the same “file.c” file, add the input-output and standard library header at the top. After that, the main method is started with the “int” return type. A character type array variable “Arr” has been declared having size 1000. The file descriptor of pointer type has been declared i.e., “f”. The file “new.txt” has been opened with the “fopen()” function of file handling with the usage of file descriptor “f”. The write mode has been used to open and write into the file.

Here comes the “if” statement. It will check if the file descriptor has not been bonded with any file and is empty, it will print that there is some error and the program will stop executing further. If the descriptor would be successfully open and create the file, the user will be asked to enter some data at the shell terminal via the “printf” clause. The “fgets()” function is a new concept here.

It is used to get the standard input entered by a user, check its size, and save it into a character variable “Arr”. The “fprintf” method is used to input this “Arr” variable into the file with its descriptor. The file descriptor gets close here.

The output is quite expected. The user has entered some sentences and the file has been displayed with the data in it.


Within this guide today, we have discussed the ways to write data into a file while working on C language. We have used different file handling built-in functions of C to do so i.e., fopen, fputc, fputs, fprintf, fgets, and fclose. On the other hand, the concept of pointers and arrays has been used as well. We extremely believe this article contains all the necessary illustrations required to understand the file writing concept in C language.

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.