Contents

Description

The purpose of this lab is to get you familiar with how to read from and write to text files in C++. We'll look at three major scenarios:

  1. searching through a file to see if it contains a given word
  2. logging user interactions to a file
  3. using a file to save state between program runs of a simple program

The third scenario is particularly helpful for PA4. In addition to those specific scenarios, by the end of this lab you will be able to:

  1. check if a file exists
  2. read from a file
  3. write to a file
  4. save program state
(Back to top)

Tasks

Complete each of the following parts in a separate file and have the professor check your work before the end of lab.

Task 1: searching a file

In this task, we'll write code that will search a text file for a user-provided word. The program will search the file and let the user know how many times the word occurs in it. Before we look at the code, lets consider an example of the program running. Here's the sample text file we'll use:

input.txt
mary had a little lamb
little lamb
little lamb
mary had a little lamb
whose fleece was white as snow

When we run the program, it should ask the user for the input text file. Then it should ask what word we want to search for. If we ask for the count of the word "lamb", then we should get a count of 4. If we ask for the word "blue", we should get a count of 0. Here's the program running:

$ ./lab10-task1
Please enter a file to search: input.txt
Please enter a word to search for: lamb
Word count of lamb: 4

Would you like to enter another word? [y]es/[n]o: y
Please enter a word to search for: blue
Word count of blue: 0

Would you like to enter another word? [y]es/[n]o: n
Goodbye, then!

Here's the incomplete code. Your job is to finish implementing the getWordCount function. The pseudo code is in place for you. Take a look at the File IO topic page to see how to read all of the words in a file.

lab10-task1.cpp

Task 2: logging to a file

A common use of files is to log data while a program is running. For instance, a web server logs all requests for web pages along with timestamps and IP addresses. In this task, we'll modify the program from Task 1 to include a log function that will write to file all of the interactions with the user. For each word that the user searches for, the log should contain a line consisting of the file searched, the word searched for, and the number of occurrences of the word in that file, each separated by a space. For example, the log produced by the interactions demonstrated in Task 1 would look like this:

log.txt
input.txt lamb 4
input.txt blue 0

Moreover, when logging, we will create a new log file if one doesn't exist, or append to the existing log if it doesn't exist. That means that after running the program several times, the log should contain the interactions across all those runs.

Here's the code. You need to paste your code from Task 1 into the getWordCount function, then implement the log function. Notice that we are using a constant for the log filename. Also, I have provided the code to open the file for appending. You just need to write the code to output the data and close the file. See the File IO topic page to see how to write to an output file stream.

lab10-task2.cpp

Task 3: saving state

Now that we know how to read and write files, a useful thing to use files to save the state of a program. For example, think about playing a video game. It's helpful if you can save your progress (what level you're at, your health level, weapons, etc.) so that you can walk away and come back later, picking back up right where you left off. That saved data is stored to some kind of file or data base.

In this task, you will finish implementing the program below. It prompts the user for their name and a list of friends. For each friend, the program asks for the friend's name and age. This information is stored within two structures, UserInfo and FriendInfo. Once the information has been entered, the program saves it and then exits. When the program is rerun, the data is loaded and displayed. The user is then given the option to edit it or not. If the user opts to edit the data, they have to enter it all again and the program saves it and exits. If the user opts to not edit it, then the program exits without making any modifications.

Your job is to finish implementing the loadInfo and saveInfo functions. Look for the TODO: tags. The only code you are writing is to read and write the friend information. Look around at the surrounding code to see how to properly use getline and the << and >> operators.

lab10-task3.cpp
(Back to top)