Comp11
I/O Library Reference

Note for comp15

This is a library of functions I developed for use in comp11. It makes reading and writing from files much simpler than using iostreams. Feel free to copy the code and use it in your assignments, or just look at the implementation to understand how it works.

Overview

All of the programs we will write require input data from either the user (at the terminal) or from a file. In addition, they will often display their results on the terminal, or store them in another file. I will provide you with a set of functions (a function library or API) for input and output. The following documentation describes these functions and shows their declarations (name, parameter types, and return type).

Download

Link to comp11io.h

Link to comp11io.cpp

You can see the complete code online.

Write output to the terminal

These functions print values of various types on the terminal, in the same way that many regular commands do. Note that if you want to print multiple lines of output, you need to use putNewline() in between.

void putInt(int i);Write an integer value to the terminal
void putBool(bool b);Write a Boolean value (either true or false) to the terminal
void putChar(char c);Write a single character to the terminal
void putDouble(double d);Write a double-precision value to the terminal
void putString(string s);Write a string of characters to the terminal
void putNewline();Output a newline

Get input from the terminal (user)

These functions are used to read input of various types from the user. Note that the string functions are not symmetric with their put counterparts. The reason is that when reading strings we need to know where to break them up. This library provides two options: read a word (a contiguous string of non-space characters), or an entire line of text.

int getInt();Get an integer value from the terminal
bool getBool();Get a boolean value from the terminal. The user can type either "true" or "yes" for this function to return a true boolean.
char getChar();Get a single character from the terminal
double getDouble();Get a double-precision value from the terminal
string getToken();Get a word (a string of non-space characters) from the terminal
string getLine();Get an entire line of text from the terminal

File access functions

Files are represented by one of two types: ifstream for reading (input) and ofstream for writing (output).

To read data from a file, start by calling openFileForReading passing in a newly declared ifstream and the name of the file. You can then call any combination of the get routines below. Remember that the ifstream keeps track of where you are in the file. It starts at the beginning, and each time you "get" something from the file, the current position advances. You can check for end-of-file with the atEndOfFile functions. When done, call closeFile.

bool openFileForReading(string name, ifstream& in);Open the file named "name" for reading and associate it with the given ifstream
int getInt(ifstream& in);Get an integer value from the given file
bool getBool(ifstream& in);Get a boolean value from the given file. The file can contain either "true" or "yes" for this function to return a true boolean.
char getChar(ifstream& in);Get a single character from the given file
double getDouble(ifstream& in);Get a double-precision value from the given file
string getToken(ifstream& in);Get a word (a string of non-space characters) from the given file
string getLine(ifstream& in);Get an entire line of text from the given file
bool atEndOfFile(ifstream& in);Check to see if all the data has been consumed

Writing a file is very similar, except that when you open a file for writing, it starts out empty. Each call to "put" appends another piece of data, in the order the program call them.

bool openFileForWriting(string name, ofstream& out);Open the file named "name" for writing and associate it with the given ofstream
void putInt(int i, ofstream & out);Write an integer value to the given file
void putBool(bool b, ofstream & out);Write a Boolean value (either true or false) to the given file
void putChar(char c, ofstream & out);Write a single character to the given file
void putDouble(double d, ofstream & out);Write a double-precision value to the given file
void putString(string s, ofstream & out);Write a string of characters to the given file
void putNewline(ofstream & out);Output a newline

Closing a file is the same for both kinds of files:

void closeFile(fstream& in_or_out);Close the given file. Using the fstream after this point will cause an error.

Back to Comp15.