5.1) Intro
5.2) Streams and FILE Objects
5.3) Standard Input, Standard Output, and Standard Error
5.4) Buffering
5.5) Opening a Stream
5.6) Reading and Writing a Stream
5.7) Line-at-a-Time I/O
5.8) Standard I/O Efficiency
5.9) Binary I/O
5.10) Positioning a Stream
5.11) Formatted I/O
5.12) Implementation Details
5.13) Temporary Files
Appendix: Examples
#include <stdio.h> void main() { /* default is standard output */ printf("Hello world\n"); }
#include <stdio.h>
void main() { /* stating file stream explicitly */ fprintf(stdout, "Hello world\n"); }
#include <stdio.h> /* Returns: 0 if OK, nonzero on error */ int setvbuf(FILE *fp, char *buf, int mode, size_t size);
#include <stdio.h> /* All three return: file pointer if OK, NULL on error */ FILE *fopen (const char *pathname, const char *type); FILE *freopen(const char *pathname, const char *type, FILE *fp); FILE *fdopen (int fileDescriptor, const char *type);
r | open for reading |
w | truncate to 0 length or create for writing |
a | append; open for writing at end of file, or create for writing |
r+ | open for reading and writing |
w+ | truncate to 0 length or create for reading and writing |
a+ | open or create for reading and writing at end of file |
#include <stdio.h> /* Returns: 0 if OK, EOF on error */ int fclose (FILE *fp);
#include <stdio.h> int fgetc (FILE *fp); /* * fgets is used to read one character at a time. * * Returns: next character if OK, EOF on end of file */ int fputc (int c, FILE *fp); /* * fputc writes one character at a time * * Returns: c is OK, EOF on error */ char *fgets (char *buf, int n, FILE *fp); /* * fgets reads a line-at-a-time. We have to specify * the size of the buffer, n. This function reads * up through and including the next newline, but no more * than n-1 characters, into the buffer. The buffer * is terminated with a null byte. If the line, including the * terminating newline, is longer than n-1, then only * a partial line is returned, but the buffer is always * null terminated. Another call to fgets will read * what follows on the line * * Returns: buf if OK, NULL on end of file or error */ int fputs (const char *str, FILE *fp); /* * fputs is generally used to write a line-at-a-time. * The function fputs writes a null terminated string to the * specified stream. The null byte at the end is not written. * Note that this need not be a line-at-a-time output, since the * string need not contain a newline as the last nonnull character * * Returns: nonnegative value if OK, EOF on error */ int ferror (FILE *fp); int feof (FILE *fp); /* * You'll notice that with fgetc and fgets that the same value is * returned regardless whether an error occurs or the * end of file is reached. To distinguish between the two, we * must call either ferror or feof. * * Both return: nonzero (true) if condition is true, 0 (false) otherwise */
#include <stdio.h> /* * Both return: number of objects read or written */ size_t fread (void *ptr, size_t size, size_t nobj, FILE *fp); size_t fwrite (const void *ptr, size_t size, size_t nobj, FILE *fp);
float data[10]; /* here we specify size = the size of each element of the array, * and nobj = the number of elements in the array */ if (fwrite(&data[2], sizeof(float), 4, fp) != 4) fprintf(stderr, "fwrite error!");
struct { short count; long total; char name [NAMESIZE]; } item /* here we specify size = the size of the structurre * and nobj = 1 (the # of objects to write) */ if (fwrite(&item, sizeof(item), 1, fp) != 1) fprintf(stderr, "fwrite error!");
#include <stdio.h> long ftell (FILE *fp); /* * gets the current file position relative to the beginning * of the file * * Returns: current file position indicator if OK, -1L on error */ int fseek (FILE *fp, long offset, int whence); /* * For binary files: * offset = byte offset, relative to whence * whence = { SEEK_SET means from the beginning of a file, * SEEK_CUR means from the current file position, * SEEK_END means from the end of file } * * For text files: * offset = { 0 (meaning rewind to beginning), * or a value previously returned by ftell * for that file } * whence = SEEK_SET * * Returns: 0 if OK, nonzero on error */ void rewind (FILE *fp); /* * Rewinds to the beginning of a file. Works with both binary * and text files */
#include <stdio.h> /* * fgetpos stores the current value of the file's position * indicator in the object pointed to by pos. This value * can be used in a later call to fsetpos to reposition * the stream to that location * * Note: they introduce a new abstract data type, fpos_t, that * records a file's position * * Both return: 0 if OK, nonzero on error */ int fgetpos (FILE *fp, fpos_t *pos); int fsetpos (FILE *fp, const fpos_t *pos);
#include <stdio.h> int printf (const char *format, ...); /* * printf writes to the standard output. * * Returns: the number of characters output if OK, * negative value if output error */ int fprintf (FILE *fp, const char *format, ...); /* * fprintf writes to the specified stream. * * Returns: the number of characters output if OK, * negative value if output error */ int sprintf (char *buf, const char *format, ...); /* * sprintf writes to the character array buf. * sprintf automatically appends a null byte at the end of * the array, but this null byte is no included in the * return value. * * Returns: the number of characters stored in the array */
#include <stdio.h> /* Returns: the file descriptor associated with the stream */ int fileno (FILE *fp);
#include <stdio.h> /* Returns: file pointer to a unique file if OK, NULL on error */ FILE *tmpfile (void);