C Library Functions -fputc()
C Standard Library - <stdio.h>
Description
C Library Functionsint fputc(int char, FILE *stream)Pass the parametercharThe specified character (an unsigned character) is written to the specified stream, and the position indicator is advanced.
Declaration
The following is the declaration of the fputc() function.
int fputc(int char, FILE *stream)
Parameter
- charThis is the character to be written. This character is passed as its corresponding int value.
- streamThis is a pointer to a FILE object that identifies the stream to which the character is to be written.
Return Value
If no error occurs, the character written is returned. If an error occurs, EOF is returned and the error indicator is set.
Example
The following example demonstrates the usage of the fputc() function.
Example
#include <stdio.h>
int main ()
{
FILE *fp;
int ch;
fp = fopen("file.txt", "w+");
for( ch = 33 ; ch <= 100; ch++ )
{
fputc(ch, fp);
}
fclose(fp);
return(0);
}
Let us compile and run the above program, which will create a file in the current directoryfile.txt, the content of which is as follows:
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd
Now let's use the following program to view the contents of the above file:
Example
#include <stdio.h>
int main ()
{
FILE *fp;
int c;
fp = fopen("file.txt","r");
while(1)
{
c = fgetc(fp);
if( feof(fp) )
{
break ;
}
printf("%c", c);
}
fclose(fp);
return(0);
}