fputc() — Write Character

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:

Value
Meaning
ECONVERT
A conversion error occurred.
ENOTWRITE
The file is not open for write operations.
EPUTANDGET
A write operation that was not permitted occurred after a read operation.
ERECIO
The file is open for record I/O.
ESTDERR
stderr cannot be opened.
ESTDOUT
stdout cannot be opened.
EIOERROR
A non-recoverable I/O error occurred.
EIORECERR
A recoverable I/O error occurred.

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.

Note:
Because the output occurs as a side effect within the second expression of the for statement, the statement body is null.
#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



[ Top of Page | Previous Page | Next Page | Contents | Index ]