myIOlib - A Simple I/O Library

1. Functionality

myIOlib is a very simple I/O library provided to simplify certain kinds of I/O operations in CSc 352 programming assignments. It provides three functions to read in lines and files, as follows:

char * myGetLine(void)
This function reads in the next line of text from stdin and returns a pointer to (a copy of) the string read in. This string contains the newline character at the end of the line. When end-of-file is encountered, it returns NULL.

A line is assumed to contain at most 4096 characters.

char ** myGetFile(void)
This function reads in the contents of an entire text file from stdin and returns a pointer to an array of pointers. Each pointer in the array points to (a copy of) one line of text: the first element (i.e., at index 0) points to the first line; the second element to the second line; and so on. The end of the array is indicated by an array element that is a NULL pointer.

A line is assumed to contain at most 4096 characters.

char ** myReadFile(char *fname)
This function is very similar to myGetFile(), except it takes as argument the name of a (readable) file.

It reads in the contents of the entire file fname and returns a pointer to an array of pointers. Each pointer in the array points to (a copy of) one line of text: the first element (i.e., at index 0) points to the first line; the second element to the second line; and so on. The end of the array is indicated by an array element that is a NULL pointer.

A line is assumed to contain at most 4096 characters.

If an error is encountered when attempting to read the file specified, the function returns NULL.

A library contains machine code; the myIOlib library is provided as machine code for the Sun SPARC processors on lectura. This code will not run on Pentiums.

2. Using the Library

In order to use this library, your program should include the header file myIO.h using

#include <myIO.h>
To do this, you will have to tell the compiler (1) where to find this include file; (2) the name of this library; and (3) where to find the library. This issue is discussed here. In particular:
Library name: myIO
Include file directories: /home/cs352/FALL02/hw4_stuff
Library code directories: /home/cs352/FALL02/hw4_stuff/lib

3. Examples

Suppose that the following lines are typed in on stdin:

The C Programming Language
Second Edition
by Kernighan & Ritchie
Then, successive calls to myGetLine() would return the following:

By contrast, a call to myGetFile() returns the following:

If the lines above are in a file foo.txt, then this is also what is returned by a call to myReadFile("foo.txt").

The file /home/cs352/FALL02/hw4_stuff/mycat.c contains an example illustrating the use of this library to implement a simple variant of the Unix utility cat.