CSCI 201 Lab 10 -- Loops and Files

In this lab you'll modify a small C++ programs. The program uses a while loop to read data from a file and then draw the "shapes" described by that data.

Cleaning up

If there is anything in C:\FILES delete it.

The Task

First, create a project of type Win32 Application in C:/FILES called FileRead. Add the EzWindows library to your project and, if necessary, modify the options under Tools to add the path to the EzWindows include directory.

Add a new C++ Source code file called FileRead.cpp to your project. Paste the program outline found on this link into FileRead.cpp.

In this lab you will write a program that reads a description of a drawing from a file and displays it in a window. Each line of the file will describe a shape using six components:

  1. X co-ordinate of the center point
  2. Y co-ordinate of the center point
  3. Width
  4. Height
  5. Color
  6. Type -- rectangle, ellipse, triangle
After these six items an optional "comment" may appear on the line. Here are a few lines that describe a couple of red rectangles followed by a blue ellipse
 5.0  6.0 3.0 2.0 r r
 7.8  3.0 4.0 3.0 r r please ignore the comment
10.0 10.0 2.0 2.0 b e

Colors and shapes are described by one letter. The encoding for colors is explained in the comments of the C++ function letter2color. The three shapes are encoded by the letters r, e, and t.

There are three places where you need to modify this program. First, insert your name into the comment on the first line.

Second, look for the comment line that reads

// Fill in here to read the line
This is where you add four more ">> variable" into the while loop condition to read in the other items of the line.

Third, do the hard part. Look for the line

// Fill in here to draw the shape
This is where you add the code that creates an appropriate shape and draws it to the screen. As a first step for this part of the assignment you might want to try drawing a rectangle of the appropriate dimensions and color. If you are unsure about how to use the function letter2color to transform the input letter into an EzWindows color, you should ask your lab instructor for help.

Once you are able to draw a rectangle with little trouble, modify your program to produce any of the three shapes encoded by the letters r, e, and t.

Sample inputfile

Your program contains the lines

ifstream fin ;
fin.open("Lab10.txt") ; 
These lines declare fin to be an ifstream object that extracts values from the file Lab10.txt. Consequently, you will need to have a drawing description stored in a file called Lab10.txt in your project directory. We have provided a sample file for cut-and-paste to help you out here. This file is a "translation" of a C++ face program written last year. Create a new Text File in your project with the name Lab10 and paste the contents of this file into it.

Lab Checkoff

Ask your lab instructor to verify that your program correctly produces the drawing specified in the sample file above.

Extra Credit

If you go through this exercise at warp speed, you might consider the following extra credit assignment. Write a simple program that reads this list of integers from one file and writes them to a different file. Hint: an input file (i.e., a file that you read from) is of type ifstream (see the program that you wrote above for an example), and an output file (i.e., a file that you write to) is of type ofstream.

Extra Credit Checkoff

After you have tested your program, ask your instructor to run it.


Return to CSCI 201 page
CSCI logo Return to the UNCA Computer Science home page