"A Rolodex is a rotating file device used to store business contact information." It consists of an axle or shaft with information cards attached at one edge. The cards spin around the axle, allowing a user to read the cards' information easily. The cards typically contain contact information such as an address and phone number. Our task is to write a simple program that implements a Rolodex.
Our program will read a text file one line at a time. Each line in the file will correspond to one Rolodex card. Each line will contain three separate data items: (a) a person's name, (b) the person's address, (c) the person's telephone number. Each data item or field is separated from the other fields by a colon character.
The program must be able to locate the colon, separate the individual fields into separate strings, and then produce a well-formatted table displayed on the screen (or in the console window). The process of separating data into individual components is called parsing and is a common part of many different computer problems.
There are many ways of parsing input. For example, compilers read a file one character at a time and assemble the characters into groups representing parts of a program. Each group of meaningful characters is called a token. Another way of parsing input is to read large blocks of text and then search for the characters that separate the tokens. The characters that separate the tokens are called delimiters. For the Rolodex problem, the straightforward format of the input file allows us to parse the input in yet a different way: We'll use an overloaded version of the getline
function to read characters from the file into a string, but the function will stop reading when it reads (and discards) the colon delimiter.
The next two sections develop two programs, the first based on the string class and the second based on C-strings, that read rolodex.txt and produce the formatted output shown below. First, create the Visual Studio projects for each program. Then copy rolodex.txt into each project folder - that is, into the folder containing the source code (.cpp) files.
If you create rolodex.txt by copying and pasting from Figure 2 or the downloadable file, make sure that there are no empty lines - even at the end of the file.