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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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 :

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.