C Language



Admission Enquiry Form

  

Write a Character(use of putc() function) C File




Write a Character into a File in C File Handling

We can also write a character into the file in file handling by using fprintf() or by using putc().

1. Example to write a character using fprintf() into a file:

#include<stdio.h>
void main()
{
char ch='c';
FILE *fptr;
fptr=fopen("compuhelp.txt","w");
printf("Writing single character to compuhelp.txt file.");
if(fptr==NULL)
{
printf("\n File could not be opened ");
}
else
{
fprintf(fptr,"%c",ch);
}
printf("\n\n Character saved to the file");
fclose(fptr);
}


Output:

Writing single character to compuhelp.txt file.

Character saved to the file

Now you can see your notepad file named as compuhelp.txt into the hard-disk.It is saved into the default directory i.e. C:\COMPUHELP\c-notes




2. Example to write a character using putc() into a file:

#include<stdio.h>
void main()
{
char ch='c';
FILE *fptr;
fptr=fopen("compuhelp.txt","w");
printf("Writing single character to compuhelp.txt file.");
if(fptr==NULL)
{
printf("\n File could not be opened ");
}
else
{
putc(ch,fptr);//use of putc function
}
printf("\n\n Character saved to the file");
fclose(fptr);
}


Output of the above code will be same as Example1