NAME

read_line, fread_line, prompt_line - easy line-based input

SYNOPSIS

#include <ipd.h>

char *
fread_line( FILE * stream );

char *
read_line( void );

char *
prompt_line( const char * format, ... );

DESCRIPTION

These three functions read a line at a time either from stdin(4), as read_line and prompt_line do, or a from file handle stream, as fread_line does.

The pointer returned by these functions is allocated using malloc(3), and thus must be freed using free(3) when you are done with it. This is because unlike other input functions, they allocate their own buffer, growing it as necessary to accept any amount of input (limited, of course, by the computer's memory).

All three functions read until reaching either end-of-file or a newline character. The newline character, if present, is not included in the resulting string. If no characters can be read before end-of-file is reached, the functions return a NULL pointer.

Additionally, prompt_line() prints a prompt to stdout(4) and flushes it to ensure it is displayed immediately even if it doesn't end with a newline. prompt_line() takes a format string and arguments to interpolate, in the style of printf(3).

ERRORS

If any of the three functions fails to allocate memory, it prints an error message to stderr(4) and calls exit(3) with an error code of 1.

BUGS

Almost all uses of prompt_line on IPD homework assignments are bugs, because none of the assignments specify prompts, and all are strict about output.

AUTHOR

Jesse Tov <jesse@cs.northwestern.edu>

SEE ALSO

fflush(3), free(3), getline(3), malloc(3), printf(3)