C Programming

Fputs in C

Like many other languages, the C language came up with many functions to perform many things. From many of those functions, one is the β€œfputs” function of C filing. Using this function within the C code is to write some text or string data within a file. One of its parameters will be a string or pointer to a string to be saved, and the other will be a file descriptor pointing towards the file. We have decided to discuss the fputs function of C file handling in today’s guide. Let’s make it now.

Let’s have a start by opening Ubuntu’s terminal shell with the keyboard shortcut β€œCtrl+Alt+T”. Within the terminal shell, we will be utilizing the apt package to update the system with an update keyword. Add your system password, press Enter, and you are good to go. Within a few seconds, our system will get updated.

Before going towards the examples of using β€œfputs” in C, we need an empty file within our system. Therefore, we have to use Ubuntu’s touch instruction within the console terminal to create a new text file quickly. Right now, we have been naming it as β€œfile.txt”. This file has been created within the system’s home folder. You can go towards the folders to see.

The file must be empty as we have just created it. Let’s confirm that it is empty. You have to use the β€œcat” instruction of Ubuntu on the console query area along with the file name. Simply type the shown below command in the image at the query area and execute it by pressing the Enter key. The output is showing nothing because the file is empty. Let’s get started with our examples now.

Example 01:

Firstly, you have to ensure that your Ubuntu system has a C compiler installed already, i.e. β€œgcc”. Without it, we will be unable to make our code executed.

Let’s start our first illustration with the creation of another file. This time, our file will be of the β€œC” type, i.e. having a C language extension. The very same β€œtouch” query will be again utilized so far to create it. You can take a look at the home folder of your Linux system to see the empty file. We have named this file β€œfputs.c”.

It is time to open this file within an editor of Ubuntu to start doing code. We have preferred the β€œnano” editor here, i.e. use nano with the file name to open it.

The empty file will be launched in Ubuntu’s nano editor. The code will be started by the C standard input output header library, i.e. stdio.h. This will be added by the β€œ#include” keyword. The main() function has been started with the declaration of the pointer type File descriptor β€œf” using the FILE stream object.

The fopen() function of the file stream will be used to open the newly made text file, i.e. file.txt in writing mode. The function return value, i.e. true/false, will be saved to file descriptor β€œf”. If the file is successfully opened, we will use the fputs() function to add some data into the file using the file descriptor β€œf” as a file stream.

We have been adding two different lines using the fputs() function along with the file descriptor β€œf’. After that, the fclose() function will be using the file descriptor to close the opened file. The main() function has been completed here to execute.

#include <stdio.h>

nt main () {
   FILE *f;
   f = fopen("file.txt", "w+");
   fputs("This is a new text file...", f);
   fputs("We are adding some data into the text file...", f);
   fclose(f);
   return(0);
}

After saving our code, we have to compile it with the β€œgcc” compiler. Run the code file after the compilation with the β€œ./a.out” shown below command. It will display nothing in return.

Let’s check out the file contents using the β€œcat” command again. You can see that two string sentences have been written to the file.

Example 02:

Let’s go through another example. The user-defined WriteData() function takes a character type variable in its argument. We have declared a file descriptor β€œf” with C Filing object FILE.

The fopen() function is here to open the text file β€œfile.txt” in the write mode and save the response within the file descriptor β€œf”. The fputs() function writes the passed string β€œs” value to the file using the file descriptor β€œf”.

The main() function has been declaring a character variable β€œs” of size 3. The string line has been copied to the variable β€œs” using the β€œstrcpy” function. The function β€œWriteData” has been called and passed the variable β€œs” in its parameters. The code has been completed here.

#include <stdio.h>

WriteData(char s[]) {
   FILE *f;
   f = fopen("file.txt", "w");
   fputs(s, f);
   fclose(f);
}
int main() {
   char s[30];
   strcpy(s, "Line will saved to text file\n");
   WriteData(s);
   return 0;
}

Make your file compiled with the β€œgcc” command and run it with β€œ./a.out” as below.

Now, check the file.txt file via the β€œcat” instruction. You can see that the string has been successfully written in the file.

Conclusion:

This was all about using the fputs function of C file handling in our C code while working in Ubuntu 20.04 system. We have discussed two examples by passing a string variable to a user-defined function and directly passing a string value to the fputs function. Both are yielding the same results, i.e. writing data in the file.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content