C Programming language

Character I/O : How to use fputc( ) Function in C
Previous Home Next

fputc() Function: This function writes a character to a specified file at the current file position and then increment the file position pointer. The putc() macro acts essentially identically to fputc(), but is a macro that expands in-line. It may evaluate stream more than once, so arguments given to putc() should not be expressions with potential side effects.

int fputc(int c, FILE *fp);

The fputc() function returns an integer representing the character written. If a write error occurs the error indicator is set and fputc() returns EOF.

/** program to understand the use of fputc( ) function */

#include<stdio.h>
void main()
{
FILE *p;
char ch;
if((p==fopen("myfile.txt","w"))== NULL)
{
printf("this file does not exist\n");
exit()
}
else
{
printf("enter the text")
while((ch==getchar())!=EOF)
fputc(ch,p);
}
fclose(p);
};

Previous Home Next