C Programming

Truncate File Linux in C Lang

As clear from the name “truncate”, it means removing, clearing up, or reducing size. There are many ways available to truncate a file while you are working on the Linux operating system. You can use the ftruncate() method to reduce the document’s size specified by the uncluttered document descriptor to the specified value. This article will let you know about the method of the “TRUNC” function to truncate a file using the “C” language code while using the Ubunutu 20.04 Linux system.

Example: Truncate a File

Most of our work would be on the command-line terminal. So, we have to open it first using the “Ctrl+Alt+T” shortcut key while we are on the desktop of the Ubuntu 20.04 Linux system. Another way to open the shell terminal is to navigate to the Activity menu bar at the top left corner of the Ubuntu 20.04 Linux system. Click on it, and you will get a search bar. Write the name of an application, for example, “terminal”, and hit the “Enter” key from the keyboard typewriter. The command terminal will be opened in seconds and ready to be used. Now, we have first to create a new text-type document or file via terminal. To create any type of file, Linux-based operating systems use the “touch” command to do so. Therefore, we are also using the touch command to create one having the name “test.txt” and click on the “Enter” button.

$ touch test.txt

You can see that nothing will happen. If you want to see the newly created file, you have to open the file and folder directory. You will find a “test.txt” file in the Home directory of your Linux operating system, as presented in the image snapshot below.

Now, the file must have some data to be used for further use. So, open the file by double-tapping it. The file will be opened, and you have to write some text content in it. After you have done, click on the “Save” button or simply use the shortcut key “Ctrl+S” to save the file. Close the file by hitting on the cross sign.

Let’s come to the terminal again. If somebody wants to check if the file “test.txt” reflects the change, they can do it as well. Simply try out the “cat” instruction in the console terminal with the name of a file, “test.txt,” and press the “Enter” key from your keyboard. The output below is reflecting the updated changes of a file.

$ cat test.txt

As we have done with creating and updating text files, it’s time to write some C language code to truncate this file. But to compile C language code, we must have some C language compiler installed and configured on our Linux system. So, we have installed the “gcc” compiler on our Ubuntu 20.04 Linux system using the below query. It requires the current account password to install it. Type the account password and hit the “Enter” button.

$ sudo apt install gcc

After installing a “gcc” compiler, we can now create any “C” file. So that, we are creating a “C” file named “test.c” with the “nano” command to open it in a GNU editor. Press the key “Enter” to continue.

$ nano test.c

Now the “GNU” editor nano-type file has been opened. We can write a “C” code in it. Write out the below same code in it. The code contains some header files necessary to be used for the truncation of a file “test.txt” specially “unistd.h” and “sys/file.h” header. After that, we have initiated a “main” function having return type “integer” as “int”. Then, we have declared two integer type variables, “n” and “fd”. After that, we have used the character type array named “buf” of size “20”. In the next line, we have used the open system call to open the file “test.txt” that we have created above. We have also attached two flag parameters in this function call. The  O_RDONLY flag has been used to read the data from the file “test.txt”, and the flag O_TRUNC has been used to remove or clear up the contents of a file to zero sizes. Then we have used the read and write system calls to read and write the contents from the buffer “buf” array and display them on the screen using the write system call. Save this C code using the shortcut “Ctrl+S” and quit the nano editor using the “Ctrl+X” control.

Now it’s time to use the gcc compiler to compile the “C” code we have just wrote in the nano editor. So, type the below “gcc” query in the console terminal along with the name of a file as “test.c” and hit the key “Enter” from your desktop keyboard.

$ gcc test.c

Now run the code using the “a.out” instruction as shown below. The output is showing some garbage value in it.

$ ./a.out[cc]

<img class="alignnone size-full wp-image-100108" src="https://linuxhint.com/wp-content/uploads/2021/05/image3-3.png" alt="" width="388" height="39" />

Now, let’s check the test.txt file if it has got changes due to the C code. Type the below “catcommand to see the contents of a file and press “Enter”. The query output doesn’t show anything; this means that the file has been efficiently and successfully truncated via the C code.

[cc lang="bash" width="100%" height="100%" escaped="true" theme="blackboard"]$ cat test.txt

We can also check the file “test.txt” via the manual way. Open the home directory of your Linux system and double click on the file “test.txt” to open it. You can see that the file has no contents left in it. This means our C code has been worked greatly for truncate.


At last, we have done the topic of truncating a file using the C language code in the Ubuntu 20.04 Linux system. We hope this article has helped you a lot in understanding the concept of truncate. All the queries work properly in any distribution of the Linux system.

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.