Programming in C

(Barry) #1
I/O Functions 477

size_t fwrite (buffer, size, n, filePtr)
Writes nitems of data from bufferto the specified file. Each item of data is size
bytes in length. Returns the number of items successfully written.
int getc (filePtr)
Reads and returns the next character from the indicated file.The value EOFis
returned if an error occurs or if the end of the file is reached.
int getchar (void)
Reads and returns the next character from stdin.The value EOFis returned upon
error or end of file.
char *gets (buffer)
Reads characters from stdininto bufferuntil a newline character is read.The new-
line character is notstored in buffer, and the character string is terminated with a
null character. If an error occurs in performing the read, or if no characters are read, a
null pointer is returned; otherwise,bufferis returned.
void perror (message)
Writes an explanation of the last error to stderr,preceded by the string pointed to
by message.For example, the code
#include <stdlib.h>
#include <stdio.h>

if ( (in = fopen ("data", "r")) == NULL ) {
perror ("data file read");
exit (EXIT_FAILURE);
}
produces an error message if the fopencall fails, possibly giving more details to the
user about the reason for the failure.
int printf (format, arg1, arg2, ..., argn)
Writes the specified arguments to stdout, according to the format specified by the
character string format(see Chapter 16). Returns the number of characters written.
int putc (c, filePtr)
Writes the value of cas an unsigned charto the indicated file. On success,cis
returned; otherwise EOFis returned.
int putchar(c)
Writes the value of cas an unsigned charto stdout,returning con success and
EOFon failure.

21 0672326663 AppB 6/10/04 2:03 PM Page 477

Free download pdf