Writing the information to file is also major function. fputc() function in C is used to write characters to file.

What is fputc() function in C?

For using the fputc() function in C we have to include stdio.h header file to our program.
fputc means file put character is a file handling function in C programming which used to write a single character.
This function used to write the characters to file or stream.

fputc() function in C
fputc() function in C

Syntax for fputc() function in c
fputs(‘character’,file_pointer);

In the above syntax for fputc function, we can see the fputc function accepts two parameters one is the character which we want to write to file and second is a file pointer. file pointer is a pointer to File object which holds the address of the file (location of file ) and the operation mode in which we opened file (that is read, write or append etc).

Program to write to file using fputc() function in C

#include<stdio.h>

#include<conio.h>

void main ()

{

FILE *fp;. // Creating File object

char ch='S';

printf ("\n opening file in write mode to write character to file:");

fp= fopen("technical.txt","w");. // tech.txt is file which we opening in write mode

printf("\nWriting to file:");

fputc (ch,fp); //writing character to file

printf ("charater we written to file = %c\n",ch);

fclose ();

}

Program description: –
In the above program, we are writing a single character to the file.
Let’s assume the file technical.txt present at the location.
First, we open the file from the location in write mode as we writing to file.
We take the character in the variable which we want to write.
Now, we write fputc function having parameter first the character variable which contains the character and second as a pointer to file object which holds the location of file and mode.
fputc function writes the character to the first position the file pointer is pointing.

Note:- This program we only have written one character to file.

Output: –

fputc() function in C

In the above program, we only have written one character. now we will see writing multiple character (statement) to file.

Program to write multiple character to file in c

#include <stdio.h>

int main()

{

FILE *fp;

printf ("\n opening file in write mode to write character to file:");

fp = fopen("technical.txt", "w");

char ch[10]="Defineloop";

printf("\nWriting to file:\n");

int i;

int len = strlen(ch);

if (fp != NULL) {

for (i = 0; i < len; i++) {

printf("Character we are writing to the File = %c \n", ch[i]);

// fputc to write character to file

fputc (ch[i], fp);

}

printf("\n We have written to file successfully");

fclose(fp);

}

else {

printf("\n Unable Open File");

}

return 0;

};

Program description: –
In the above program, we are writing a statement (multiple character) to file.
First, we opened the file in write mode.
In file is exists it opened in write mode.
Let us assume technical.txt file exists.
We are taken the string to want to write in the file in an array of characters.
We calculate the length of the array to store all characters in string till the end.
Next, we use for loop to iterate loop till the length of the array and write each character till the end one by one.
As pointer moves to the new position, we write character using fputc function.

Output: –

fputc() function in C

Write A Comment