Previous Table of Contents Next


3.10.1.4. Controlling Buffering

setbuf, setvbuf

void setbuf(FILE *fp, char *buf)
void setvbuf(FILE *fp, char *buf, int mode, size_t size)

These functions are used to request specific buffering strategies for the stream fp. They must be used after a stream is opened but before any I/O is performed on it.

For setvbuf, the mode argument is a constant specifying the kind of buffering to be performed. The constants (defined in <stdio.h>) are as follow:

_IONBF Indicates no buffering; characters are always read or written immediately.
_IOFBF Indicates full buffering; characters are read from or written to an intermediate buffer, which is filled or emptied all at once.
_IOLBF Indicates “line buffering”; an output stream is partially buffered but is flushed whenever a \n character is printed.

If buf is a non-null pointer, it points to a caller-supplied buffer (a character array of size size) that should be used instead of the default buffer normally allocated by the stdio library.

setbuf(fp, buf) is roughly equivalent to

setvbuf(fp, buf, buf == NULL ? _IONBF : _IOFBF, BUFSIZ)

That is, if buf is null, the stream is unbuffered; otherwise, it is fully buffered using the buffer buf with a size of BUFSIZ (where BUFSIZ is a default buffer size defined in <stdio.h>).

3.10.1.5. Error Handling

feof

int feof(FILE *fp)

feof returns nonzero if end-of-file has occurred on the stream fp; 0 otherwise. It can be used to distinguish between error and end-of-file conditions when some other function has returned an exception value. (Notice that feof reports whether end-of-file has already occurred on a previous input operation; it does not predict whether the next operation will succeed. C is very different from Pascal in this respect.)

ferror

int ferror(FILE *fp)

ferror returns nonzero if an error has occurred on the stream fp; 0 otherwise. It can be used to distinguish between error and end-of-file conditions when some other function has returned an exception value, or to detect an error after a string of unchecked output function calls.

clearerr

void clearerr(FILE *fp)

clearerr clears the error and end-of-file indications (those checked by ferror and feof) on the stream fp.

perror

void perror(const char *prefix)

perror prints a message to stderr relating to the most-recent error. The message is the same one that would have been returned by the strerror function (see section 3.10.2.6) called with the current value of the global errno. If prefix is non-null, it is prepended to the message. In other words, perror(p) is roughly equivalent to

fprintf(stderr, “%s: %s”, p == NULL ? “” : p, strerror(errno));

3.10.1.6. Operations on Strings

sprintf, sscanf

int sprintf(char *buf, const char *fmt, …)
int sscanf(const char *buf, const char *fmt, …)

sprintf and sscanf are variants of printf and scanf. Rather than working with streams, these functions work with strings. sprintf writes formatted characters to its initial buf argument, where the buffer pointed to by buf is assumed to be big enough to hold the generated string. Similarly, sscanf reads and converts characters from its buf argument, where the end of the string (\0) is interpreted as end-of-file.

3.10.1.7. Operations with Variable-Length Argument Lists

vprintf, vfprintf, vsprintf

int vprintf(const char *fmt, va_list argp)
int vfprintf(FILE *fp, const char *fmt, va_list argp)
int vsprintf(char *buf, const char *fmt, va_list argp)

These functions allow the functionality of printf, fprintf, and sprintf to be used within functions that accept their own printf-like format arguments and additional variable arguments. In each case, they accept a fmt string and a “pointer” to a variable-length argument list argp (that is, rather than an actual variable-length argument list) and generate formatted output to stdout, to the stream fp, or to the string pointed to by buf, respectively. (See section 3.10.10 for descriptions of the va_list type and the standard facilities for manipulating arguments in variable-length argument lists.)

For example, here is an error-printing function that might be used in a compiler. It prints an error message, perhaps incorporating additional arguments, preceded by the program’s name and the current filename and line number (which are passed in as global variables).

#include <stdio.h>
#include <stdarg.h>
extern char *progname;         /* program name */
extern char *filename;         /* current input file name */
extern int lineno;             /* current input line number */
void error(char *msg, …)
{
       va_list argp;
       va_start(argp, msg);
       fprintf(stderr, “%s: %s, line %d: “,
              progname, filename, lineno);
       vfprintf(stderr, msg, argp);
       fprintf(stderr, “\n”);
       va_end(argp);
}


Previous Table of Contents Next