fputs() function in C is used to write data to the file. it is similar to puts() in C. Rather than writing a string to output stream like puts() does, fputs() function writes a string to file.

What is fputs() function in C?

fputs (file put string function)function is used to put or write the string to file.
fputs() is a file handling function in C which is used to write the content to file as array of character or string.

Like fputc function fputs() function in C used to write data to file but fputc write a single character whereas fputs used to rite string to file.

fputs writes data to a specified file. In c programming for using fputs function, you have to include stdio.h header file.

fputs() function in C
fputs() function in C

Syntax for fputs() function in C: –
fputs(char array[],file_pointer);

In the above syntax fputs function accepts two parameters which are as follow:

1. The first parameter (char array) is a character array which used for storing string we are writing to file.
2. The second parameter (file _pointer):- This is the File object which holds the location of the file and opening mode in which file opened.

Program to write in a file in c using fputs function

#include<stdio.h>
#include<conio.h >
int main()
{
FILE *fp;
printf("\n opening the File in write mode:");
fp = fopen("technical.txt", "r"); //open file in write mode
char data[50]="Learn c programming:";
printf("\n Writing to file:");

if (fp != NULL) {


// fputs to write string to file
fputs (data , fp);
printf("\n We have written to file successfully");
}
else {
printf("\n Unable to Open the File");
}
printf("\n Closing File:");
fclose(fp);


return 0;
}

Output: –

fputs() function in C

Program description: –

In the above program, we are writing a string to file.

1. First, we open the file in write mode using fopen function which accepts file object and mode.
2. Character array with data we want to store in a file.
Let us assume technical.txt file exists and opened successfully in write mode.
3. Now file opened and the pointer points to the first location in a file.
4. Next, we write string which stored in variable data to file.
5. We have written fputs function to write string to file which accepts to the parameter. First is string (i.e data) and the second is file pointer which holds the location of file and mode.
6. After the program ends we can see data stored to file.

Write A Comment