LAB 09B - WRITING DATA FILES

Concepts

The focus on this assignment is on one main concept: how to write data to an "output file stream" or ofstream object.

Writing Data to a File

In class today, we learned about how data can be written to files. Remember that whenever you work with a file stream as output, we call them ofstream objects.

Similar to reading files, there will always be four things you will do whenever working with an ofstream. Open the file, check for error, write some data, and close the file. The typical pattern for this is as follows:

#include <fstream>
....

ofstream myCatsAges("filename"); // open the file

// check for an error
if ( myCatsAges.fail() ) {
  cerr << "Error opening output file";
  exit(1);
}

// write some data
myCatsAges << "3" << endl;
myCatsAges << "12" << endl;
myCatsAges << "21 woohoo!" << endl;

myCatsAges.close(); // close the file

Remember, once you have an ofstream object (like myCatsAges shown above) you use it in a manner similar to using cout.

Instructions

The cows have been kidnapped by aliens! The only clue to their whereabouts is a strange "ciphered" message, stored in the file secretMessage.txt. Fortunately, our in-house cryptanalysis expert, D. Cipher, has discovered the key:

"The key isn't very advanced, mmmkay? To decipher the message," she says, " you should take each character and replace all ~ (tilde) characters with a space, and shift all characters up by one, mmmkay?"

Your goal for this assignment is to create a program that reads the ciphered text file and then writes a deciphered version to a file called decipheredMessage.txt. For each character in the file, your program should implement the following replacement algorithm:

To see if your implementation works, you should be able to open the file decipheredMessage.txt and see the information about the missing cows.

Hints:

Where to place the input file

Any input file needs to be placed at the project level, which should be the same directory as your main.cpp file. You should see both your input file AND a .vcxproj file in the same directory.

Reading whitespace characters

In order to capture and replace whitespace characters, you will not use the >> operator with the input filestream. Instead, you will use the get() function like this:

while ( secretMessage.get(c) )
{
    // ...mmkay
}

This example assumes your ifstream is called secretMessage and you have a char variable declared that is called c.

Selection statement

Note that one requirement is to model the logic of the deciphering algorithm using a proper if/else-if/else construct. A switch statement could also be used.

Casting to char

Remember, cout and ofstream objects are sensitive to the datatype of the value to be printed or written to a file. Consider the following:

cout << ('a' + 2);

What is printed to the screen? The number 99. Why? Because a char plus an int yields an int, and then the int is "sent" to cout. To print the character c to the screen, you will need to use casting, like this:

cout << char('a' + 2);

Ahhh, much better.