File

File

NAME

File - Wrapper for stdio streams

SYNOPSIS

 #include <File.h> 

DESCRIPTION

The File class provides a simple wrapper around stdio streams for use with C++. It provides two kinds of convenience: Firstly, constructors and destructors manage opening and closing of the stream. The stream is checked for errors on closing, and the default behavior is to exit() with an error message if a problem was found. Secondly, the getline() method can be used for line-oriented input. It strips comments and keeps track of input line numbers for error reporting.

CLASS MEMBERS

File(const char *name, const char *mode, int exitOnError = 1)
File(FILE *fp = 0, int exitOnError = 1)
A File object can be initialized with either a filename or an existing stdio stream. In the first case, the file is opened according to mode (as if by fopen(3)). The exitOnError flag determines whether I/O errors should be treated as fatal.
~File()
Destroying a File object implies closing the associated stream.
char *getline()
Returns the next line from the input, stored in a static buffer of up to maxLineLength characters. Empty lines and lines starting with # are skipped.
int close()
Closes the stream without destroying the File object. Returns non-zero is an error condition occurs.
int error()
Returns a non-zero value if an error condition occurred on the stream.
operator FILE *()
A File object can be cast to FILE * to access the underlying stdio stream.
ostream &position(ostream &stream = cerr)
Outputs the current line number on stream. The stream is returned so it can be used as the left operand of the << operator.
const char *name
The filename used in creating the File object.
const unsigned lineno
The current line number as maintained by getline().
int exitOnError
When set to true this causes errors on the stream to be handled by program termination (after printing an error message).

SEE ALSO

stdio(3)

BUGS

Many other potentially useful functions are not provided (yet).

AUTHOR

Andreas Stolcke <stolcke@speech.sri.com>.
Copyright 1995, 1996 SRI International