CS 101 Lecture Notes

Week Thirteen, Monday/Tuesday: File I/O Continued

Student Evaluations

I need a volunteer to go to the engineering office in OHE and pick up the survey forms.

Program Example: Sequential Input File

In last week's example we saw how to save words input by the user to a binary dictionary file. Building a dictionary that way is a lot of work for the user. It's possible to automate part of the process by reading existing document (text) files.

The example program opens an input file by first prompting the user to enter the file specification (drive, path, file name, file extension). The file is then opened for read access and each character is read sequentially until the end of the file (EOF) is encountered. Here's the source code.


Program start-up screen.

The program reads the input file a character at a time and "parses" the input: it detect word boundaries and inserts contiguous "alphanum" sequences as words into the dictionary array. If a word is duplicated, it increments an occurrence counter.


After the input file is read. Input characters
are echoed to the screen.

The user may open many different files to build a dictionary up to 10,000 words. The word list can be displayed with the number of occurrences for each word.


In this example, the word "of" is number 162 in
the dictionary and it occurs 18 times in the input.


This page established March 16, 1999; last updated April 3, 1999.