fputc & fgetc

fputc() & fgetc() in c

fputc() & fgetc()

Declaration: int fputc(int c, File *fptr); This function writes a character to the file at the current file position then after increment the file indicator. on successfully it returns an integer representing the character written, and on error it returns EOF.

/* program to understand the use of fputc() */
#include<stdio.h>
#include<stdlib.h>
int
main()
{
FILE *fptr;
int ch;
if((fptr=fopen("filename","w"))==NULL)
{
printf("Error file can't open");
exit(1);
}
printf("Enter string \n");
while((ch=getchar())!=EOF)
{
fputc(ch,fptr);
fclose(fptr);
return 0;
}
}

fgetc()

Declaration:
int fgetc(FILE *fptr);

This fgetc(FILE *fptr) read a single character from a file and incrment the file position indicator on the sucess, it returns character after convert into the integer. or error or on end of file it returns EOF let us take example

/* program to understand the use of fgetc() */
#include<stdio.h>
#include<stdlib.h>
int
main()
{
FILE *ptr;
int ch;
if((ptr=fopen("myfile","r"))==NULL)
{
printf("Error when open file \n");
exit(1); }
while(ch=fgetc(ptr)!=EOF)
{
printf("%c",ch); }
fclose(ptr);
return 0;
}