gtpc2m36C/C++ Language Support User's Guide

fputc-Write a Character

This function writes a character.

Format

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

c
The character to be written.

stream
The stream on which the character c is written.

This function converts c to an unsigned char, writes c to the output stream pointed to by stream at the current position, and advances the file position appropriately. The fputc function is identical to the putc function, but is always a function because it is not available as a macro.

If the stream is opened with one of the append modes, the character is appended to the end of the stream regardless of the current file position.

The fputc function has the same restriction as any write operation for a read immediately following a write, or a write immediately following a read. Between a write and a subsequent read, there must be an intervening flush or reposition. Between a read and a subsequent write, there must be an intervening reposition unless an end-of-file (EOF) has been reached.

Normal Return

If successful, the fputc function returns the character written.

Error Return

A return value of EOF indicates an error.

Programming Considerations

 The TPF system does not support creating, updating, or deleting files in 1052 or UTIL state. Special files may or may not be writable in 1052 or UTIL state depending on the device driver implementation. 

Examples

The following example writes the contents of the buffer to a file called myfile.dat. Because the output occurs as a side effect in 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("myfile.dat", "w"))!= NULL )
  {
    /* Put buffer into file */
    for ( i = 0; ( i < sizeof(buffer) ) &&
           ((ch = fputc( buffer[i], stream)) != EOF ); ++i );
    fclose( stream );
  }
  else
    printf( "Error opening myfile.dat\n" );
}

Related Information

See Appendix E, Programming Support for the TPF File System for more information about TPF File System C Functions.