fputc in C Programming

The C fputc function is useful to write the character(s) to the specified stream at the current position in a file and then advanced the pointer position.

How to write the character, or character array (string), or string data to a File using fputc in C Programming with example?. Use the fputs function to write the complete string. The Syntax of the fputc is

int fputc(int char, FILE *stream)

or

int fputc(int char, <File Pointer>)

We use the empty file to demonstrate the fputc function. You have to include the #include<stdio.h> header before using this fputc function.

fputc in C Programming Example

The fputc function used to write the character(s) to the user-specified file. This C program will help you to understand the same.

// C fputc function example

#include <stdio.h> 
#include<string.h>

int main()
{
   FILE *fileAddress;
   fileAddress = fopen("sample.txt", "w");
   char name[50] = "Tutorial Gateway";
   int i;
   int len = strlen(name);

   if (fileAddress != NULL) {
	for (i = 0; i < len; i++) {
           printf("Character we ar writing to the File = %c \n", name[i]);
	   // Let us use our fputc
	   fputc (name[i], fileAddress);
	}
	printf("\n We have written the Name successfully");
	fclose(fileAddress);		
   }
   else {
  	  printf("\n Unable to Create or Open the Sample.txt File");
   }
   return 0;
}
FPUTC in C Programming 3

Let us open the File to see whether fputc returned the characters or not.

FPUTC in C Programming 4