CS1210 – C++ Programming

advertisement
CS1210 – C++ Programming
Fall 2006: Homework Assignment #15
Due: 11 Dec 2006
Points: 40
Objective: Develop a C++ program which reads an encrypted file of numbers and uses
an instructor-provided function to turn encrypted information into a recognizable image.
Discussion: In this assignment you will become familiar with file I/O and multi-file
compilation by reading and writing files using the C++ fstream library. This library
allows you to use the familiar “>>” and “<<” operators for input and output on linux
files. Examples for using the fstream library appear in display 6.2 (pg 311) and the file
I/O summary (pg 312) in your textbook. You can also find another example
(file_io_example.cpp) on our class web.
To complete this assignment your program should prompt for and read the filename of
one of a group of specially encrypted files (see the filename list below). You can find
these files at /home/shomperk/CS1210/public. Your program should then (1) read each
number in the given file, (2) use the decryption function decrypt() to convert each
number into a symbol, and (3) write this new symbol to a new output file. Once you have
decrypted the entire file, your output file should contain a recognizable object (an
aircraft). An example can be run at /home/shomperk/CS1210/public/decrypt_pic.
Encrypted File List: The following encrypted files can be found at
/home/shomperk/CS1210/public.
cessna.txt
eagle.txt
falcon.txt
harrier.txt
hornet.txt
Example: An example run of the program is shown below (the user enters the
underlined text). In the example, decrypt_pic reads the given file, eagle.txt, and converts
the numbers into symbols and writes the new decrypted file eagle_pic.txt.
john%public>decrypt_pic
Please enter a file to open: eagle.txt
Writing to output file eagle_pic.txt
There are 252 characters in file eagle.txt
Note: Your program must meet the following additional requirements:
 It must automatically generate the output file, based on the input file name, by
adding the characters “_pic” before the file extension (i.e., “.txt”).
 It must provide feedback to the user telling him/her where the output has been
written.
 It must report a count of the number of characters in the input file.
Below is a printout of the original encoded file of numbers which you can find on-line.
Your program should produce the more meaningful picture in the second file below (for
those of you familiar with military aircraft, this is a front view of an F-15 Eagle fighter).
john%public>less eagle.txt
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 0 0 0 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 4 0 7 7 7 0 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16
0 0 0 0 0 0 0 0 0 0 0 0 0 0 8 8 8 8 9 7 7 7 3 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 0 0 16
0 0 14 8 8 8 8 8 8 8 8 8 8 8 8 8 8 10 0 0 5 0 0 11 8 8 8 8 8 8 8 8 8 8 8 8 8 8 1 4 0 0 16
0 0 0 0 0 14 4 14 0 0 0 4 0 0 0 4 0 0 3 7 7 7 9 0 0 4 0 0 0 4 0 0 0 14 4 14 0 0 0 0 0 16
0 0 0 0 0 0 14 0 0 0 0 14 0 0 0 4 7 7 7 4 0 4 7 7 7 4 0 0 0 14 0 0 0 0 14 0 0 0 0 0 0 16
john%public>less eagle_pic.txt
|
|
| ___ |
----/___\---x--------------( . )--------------x
x|x | | \___/ | | x|x
x x |___| |___| x x
Additional Details:
a. Submit your source code to me by the beginning of class on 11 Dec 2006. Turn in
a listing of your program in class that same day.
b. All work must be done on james or john.
Style:
a. Please be sure to properly comment you program and use appropriate white space.
This include proper indentation.
b. Please provide meaningful, brief comments to give better understanding to the
logic of each block of your code.
c. Use meaningful variable names
Hints:
a. On your past assignments you’ve been able to build your program using the linux
command: g++ programname.cpp. Since you must now call my instructorprovided function decrypt(), you’ll need to link that code with your program. You
do this by the following:
 Copy the file crypt_func.h from /home/shomperk/CS1210/public to the place
you are writing your program (probably your home directory). Assuming you
have already changed directory to the place where your code is, type the
command: cp /home/shomperk/CS1210/public/crypt_func.h .
 Copy the file crypt_func.o (also in the public directory) as well (using the
command: cp /home/shomperk/CS1210/public/crypt_func.o .
 Compile your program using g++ as you have always before, only now you’ll
link my code with yours. For example, if your program is called hw15.cpp
you will compile it using the command: g++ hw15.cpp crypt_func.o
including the file crypt_func.o in the compile will let the compiler know that
this code is part of the final program. As in the past, this will create an a.out
file that you can execute.
b. This problem is not much more than a loop that reads a number from one file and
writes a character to another file. You can get a good start on the program by
taking a look at file_io_example.cpp (posted on the course web).
c. Make sure you test your program on more than one of the input files listed above.
Download