fputc -- Write Character

Format

#include <stdio.h>
int fputc(int c, FILE *stream);

Language Level: ANSI, POSIX, XPG4
fputc 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.

fputc is identical to putc except that it is never replaced by a macro.

Return Value

fputc returns the character written. A return value of EOF indicates an error.

Example
This example writes the contents of buffer to a file called myfile.dat.

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 FILENAME "myfile.dat"
#define NUM_ALPHA  26
int main(void)
{
  FILE * stream;
  int i;
  int ch;
  char buffer[NUM_ALPHA + 1] = "abcdefghijklmnopqrstuvwxyz";
  if (( stream = fopen("FILENAME", "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 FILENAME" );
  return EXIT_FAILURE;
  /********************************************************
     The output data file FILENAME should contain:
     abcedfghijklmnopqrstuvwxyz
  ********************************************************/
}


fgetc -- Read a Character
_fputchar -- Write Character
putc - putchar -- Write a Character
_putch -- Write Character to Screen
<stdio.h>