putc


putc

Writes a character to a file

 #include <stdio.h> int putc ( int c , FILE *fp  ); 

The macro putc( ) is similar to the function fputc( ). It writes one character to the current file position of the specified FILE pointer. The return value is the character written, or EOF if an error occurred.

Because putc( ) is a macro, it may evaluate its argument more than once. Make sure the argument is not an expression with side effectsor else use fputc( )

Example

This is a simple search-and-replace filter to eliminate back-tick characters in text files:

 int c; FILE *fp; if (( fp = fopen( "textfile", "r+" )) == NULL ) {   fprintf( stderr, "Couldn't open input file.\n" );   exit(-1); } while (( c = getc( fp )) != EOF ) // Read a character until EOF {   if ( c == '`' )              // If it's a back-tick ...   {     fseek( fp, -1, SEEK_CUR ); // back up to the place it was read from, and     putc( '\'', fp );          // replace it with a single-quote character.     fflush( fp );   } } fclose( fp ); 

See Also

fgetc( ), fputc( ), getc( ), getchar( ), putchar( ); the C99 functions to read and write wide characters, putwc( ), fputwc( ), and putwchar( ); getwc( ), fgetwc( ), and getwchar( )



C(c) In a Nutshell
C in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596006977
EAN: 2147483647
Year: 2006
Pages: 473

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net