fputc()

Writes a character to a stream.

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

c

The character to write.

stream

A pointer to a file stream.

Remarks

The fputc() function writes the character c to stream and advances stream's file position indicator. Although the c argument is an unsigned int, it is converted to a char before being written to stream. fputc() is written as a function, not as a macro.

If the file is opened in update mode (+) the file cannot be written to and then read from unless the write operation and read operation are separated by an operation that flushes the stream's buffer. This can be done with the fflush() function or one of the file positioning operations ( fseek(), fsetpos(), or rewind()).

This facility may not be available on configurations of the EWL that run on platforms without file systems.

fputc() returns the character written if it is successful, and returns EOF if it fails.

Listing: Example of fputc() usage

#include <stdio.h>

#include <stdlib.h>

int main(void)

{

FILE *f;

int letter;

// create a new file for output

if (( f = fopen("foofoo", "w")) == NULL) {

printf("Can't create file.\n");

exit(1);

}

// output the alphabet to the file one letter

// at a time

for (letter = 'A'; letter <= 'Z'; letter++)

fputc(letter, f);

fclose(f);

return 0;

}

Output to file foofoo:

ABCDEFGHIJKLMNOPQRSTUVWXYZ