Format
#include <stdio.h>
int fputc(int c, FILE *stream);
Language Level: ANSI
Threadsafe: Yes.
Description
The fputc() function converts c to an unsigned char and then writes c to the output stream at the current position and advances the file position appropriately. If the stream is opened with one of the append modes, the character is appended to the end of the stream.
The fputc() function is identical to putc(); it always is defined as a function call; it is never replaced by a macro.
Return Value
The fputc() function returns the character that is written. A return value of EOF indicates an error.
The value of errno can be set to:
The fputc() function is not supported for files that are opened with type=record.
Example that uses fputc()
This example writes the contents of buffer to a file that is called myfile.
#include <stdio.h>
#define NUM_ALPHA 26
int main(void)
{
FILE * stream;
int i;
int ch;
char buffer[NUM_ALPHA + 1] = "abcdefghijklmnopqrstuvwxyz";
if (( stream = fopen("mylib/myfile", "w"))!= NULL )
{
/* Put buffer into file */
for ( i = 0; ( i < sizeof(buffer) ) &&
((ch = fputc( buffer[i], stream)) != EOF ); ++i );
fclose( stream );
}
else
perror( "Error opening myfile" );
}
Related Information