Chapter Contents

Previous

Next
putc

putc



Write a Character to a File

Portability: ISO/ANSI C conforming, UNIX compatible


SYNOPSIS
DESCRIPTION
RETURN VALUE
IMPLEMENTATION
EXAMPLE
RELATED FUNCTIONS
SEE ALSO


SYNOPSIS

#include <stdio.h>

int putc(int c, FILE *f);


DESCRIPTION

putc writes a single character c to the stream associated with the FILE object addressed by f .


RETURN VALUE

putc returns the output character or EOF if an error occurs.


IMPLEMENTATION

putc is implemented as a built-in function. A subroutine call is executed only if no output buffer is allocated, the output buffer is full, or a control character is written.

The code generated for putc normally includes tests for a NULL FILE pointer and for a stream that failed to open. If you define the symbol _FASTIO using #define or the DEFine compiler option before including <stdio.h> , no code is generated for these checks. This enables you to improve the performance of debugged programs that use putc .


EXAMPLE

This example copies characters from an input file to an output file, and it writes a blank after each period:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define _FASTIO  /* Improve getc/putc performance. */

main()
{
   FILE *infile, *outfile;
   char filename[60];
   int c;

   puts("Enter the name of your input file:");
   memcpy(filename, "tso:", 4);
   gets(filename+4);
   infile = fopen(filename, "r");
   if (!infile){
      puts("Failed to open input file.");
      exit(EXIT_FAILURE);
   }
   puts("Enter the name of your output file:");
   memcpy(filename, "tso:", 4);
   gets(filename+4);
   outfile = fopen(filename, "w");
   if (!outfile){
      puts("Failed to open output file.");
      exit(EXIT_FAILURE);
   }
   for (;;){
      c = getc(infile);
      if (c == EOF) break;
      c = putc(c, outfile);
      if (c == '.') putc(' ', outfile);
   }

   fclose(infile);
   fclose(outfile);
}


RELATED FUNCTIONS

fputc , putchar


SEE ALSO


Chapter Contents

Previous

Next

Top of Page

Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.