fputc and fgetc in c

fputc() is used to write a single character to a file. the controller will step into next position automatically .when there is n error this function returns null value \0 .we can’t write a single character and a character by reopening the file again, so we should use a loop until it detects null value

to give null value as a user (press Ctrl+Z in DOS and Ctrl+Din linux )
#include
#include
int main()
{
int ch;
FILE *fp;
fp = fopen("file.txt", "w");
if(fp == NULL)
{
printf("Error opening file\n");
exit(1);
}
printf("Enter text: ");
while( (ch=getchar()) != EOF )
{
fputc(ch, fp);
}
fclose(fp);
return 0;
}

fgetc function is used to read a character from a file and increments the position of a controller to the next one.on failure it returns EOF or –1 ,we can use read function to read from the file

#include
int main()
{
int ch;
FILE *fp;
fp=fopen("file.txt","r+");
printf("entere text");
while((ch=getchar(fp))!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
}