fputs in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In programming, we often need data in the file to use it more often and keep it stored even after the execution of the program. There are various methods provided in the C programming language for file handling. The fputs in C is one of the file handling methods which is used to write the string of characters inside the file.

What is fputs() in C ?

The fputs() in C is a method and a part of stdio which is also known as the standard input-output stream. We need to include the stdio.h header to use the fputs() method in our program. This fputs() in C is used for writing the string or the array of characters in a file.

Syntax of fputs() in C

The syntax of the fputs() method in the C programming language is as shown in the following code snippet :

Parameters of fputs() in C

The fputs() method in C takes the 2 parameters as shown in the syntax.

  • str :
    It is a string or array of characters to be written in the file.
  • stream :
    It is a pointer to the FILE object where the string is to be written.

Return Value of fputs() in C

The return type of the fputs() method in C is an integer. This method returns 0 on successful execution. Otherwise, the fputs() method returns the EOF (End of File) or -1 in case of an error in execution.

Example

Following is the coding example to write the string in the new file myFile.txt using the fputs() in C.

Code Example :

Output :

Data in myFile.txt :

In the above coding example, we included the stdio.h library which has the definition of various methods required to perform the input-output operations. The fopen() method on line number 9 opens the file to write the data, this function takes 2 parameters the file name and the mode in this case mode is w i.e. write mode. If the file name mentioned is not present then the new file is created in the write mode.

On line number 18, the fputs() method is used which takes 2 arguments as discussed above the string or array of characters to write in the file and the pointer of the FILE object where the string is to be written.

The fclose() method on line number 21, is used to close the file which we opened earlier using the fopen() method.

More Example

Writing Multiple Line Using fputs()

Unlike other methods like puts() the fputs() write strings continuously one after another without going to the next line as you can see in the following example :

Code Example :

Output :

Data in myFile.txt :

As you can see from the data in myFile.txt the strings which we passed from the fputs() methods are get written one after another.

Conclusion

  • To store data permanently even after the program execution we create the files.
  • The C programming language provides a variety of methods for file handling.
  • The fputs in C is the method used to write the string or array of characters in the file.
  • The fputs() method takes 2 parameters, one is the string to write, and the second is the pointer of the FILE object where we have to write the string inside the file.
  • All the strings get written one after another when we use multiple fputs() methods in a program as demonstrated above.