CSC110 Assignment 3 2/25/2003

Name ______

Lab Assignment 3: Simple File I/O

Objectives of this Lab

The purpose of this lab is to:

·  give you experience with simple file input and output;

·  teach you how to manipulate multiple files simultaneously;

·  give you experience searching a file for a specific record;

·  let you practice the elements of good coding style, including commenting, proper indentation, and descriptive variable naming.

Programming Assignment

The instructions for your assignment are below. Please read each one carefully and thoroughly before you begin; this will end up saving you time. Hand in copies of your program code and any print-outs that were requested.

1.  Download the materials for this lab. In this lab, you will write a program that reads and writes file data; this will be the first program you write where you will design the user interface. Therefore, all you need to download is the file cereal.txt, which is a comma-delimited ASCII text file we will use in this lab.

2.  Take stock of your situation. Have you ever found yourself in the breakfast cereal isle of the supermarket, completely dumbfounded by the dozens of cereals to choose from? There are so many choices to make! For instance, do you want a cereal with low fat, or do you want one that has low calories? In this lab, you will search through cereal data (provided in a file) for cereals with certain characteristics.

The file cereal.txt contains data on over 70 different breakfast cereals. Each line is a single breakfast cereal record, consisting of several comma-delimited fields. These fields appear (in order) below: Argument names are suggested (like) this

1.  cereal name (cname)

2.  manufacturer (mfr) (e.g., Kellogg's; manufacturers are represented by their first initial: A=American Home Food Products, G=General Mills, K=Kelloggs, N=Nabisco, P=Post, Q=Quaker Oats, R=Ralston Purina)

3.  kind (cold/hot) (type)

4.  calories (number) (calories)

5.  protein(g) (protein)

6.  fat(g) (fat)

7.  sodium(mg) (sodium)

8.  dietary fiber(g) (fiber)

9.  complex carbohydrates(g) (carbo)

10. sugars(g) (sugars)

11. display shelf (1, 2, or 3, counting from the floor) (shelf)

12. potassium(mg) (potass)

13. vitamins and minerals (0, 25, or 100, respectively indicating 'none added'; 'enriched, often to 25% FDA recommended'; '100% of FDA recommended') (vitamins)

14. weight (in ounces) of one serving (serving size) (weight)

15. cups per serving (cups)

For example, the first record in the file cereal.txt looks like this:

"100% Bran",N,C,70,4,1,130,10,5,6,3,280,25,1,0.33

So, we know that the name of this cereal is ``100% Bran'', that it is manufactured by Nabisco, that it is a cold cereal, that each serving has 70 calories, and so on. I encourage you to open the cereal.txt file in Notepad so you can become familiar with its format.

3.  Set up your program. In this assignment, you will need to obtain some input from the user, you will need to have command buttons, which (a) Print the file to the printer, (b) will cause your code to be executed, and you will need to be able to print some data to the screen. Create a simple executable program with one form, which satisfies these needs. Your grade will not depend upon how beautiful your form is; so don't waste time making it beautiful. You will need to write procedures as described below, and these can reside inside the same file with your event procedures.

4.  Write code, which prints out the selected fields from the content of the file. Declare a procedure called PrintCerealFile which takes as an argument the name of the cereal file. (The value of this argument should come from a field on the form.) This procedure should open the file and print the first 7 arguments from each of the cereal records to the screen in a readable format, e.g., something like

Name Mfg. Type Cals Prot Fat Sodium

100% Bran Nabisco (cold) 70 4 g 1 130

A use a picture box for your outputs. Be sure to use headers and the tab function to make your output more readable.

Write code that searches the file and prints out the information for one cereal. Declare a procedure called PrintCerealRecord which takes as arguments the name of the cereal file and the name of a breakfast cereal. (Both of these values should come from form fields.) This procedure should open the file and print to the picture box on the form only the record which corresponds to the cereal named by the second argument.

Use a separate picture box for this display. (required)

Write code that creates a new file based upon the original file. Declare a procedure called CreateSweetFile that takes as arguments the name of the cereal file and the name of another (as-yet nonexistent) file. (Again, both of these values should come from fields on the form.) This procedure should open both files (the first for input, and the second for output) and it should write the names of all cereals into the output file which are sweet. A breakfast cereal is sweet if it has more than 10 grams of sugar in it, or, if it has more than 10 grams of complex carbohydrates. (Be careful: don't write the same cereal name twice! Also, be wary of opening the same file for output twice; this may cause problems.)

Use a separate picture box for this display (required)

Turn in your assignment. In addition to your homework assignment and program code, hand in printouts of screenshots: one which shows all the picture boxes full of data. This will show PrintCerealFile working, and PrintCerealRecord working. If your program prints something different from what you expected or desired, give me your best guess as to what went wrong. Finally, hand in a copy of the output file containing sweet cereals that your program made.

Also submit your Flowchart, pseudocode, task list, user interface with objects clearly identified, and object sheet. This is worth (10) of your 25 points.

Homework Assignment

Think carefully about each of the following questions. Hand your answers typed with the programming assignment. You may discuss the questions with your partner, but you must write the answers yourself.

1.  Did you have any difficulties in completing the programming assignment? If so, please tell me about them, and any solutions you may have found.

2.  Imagine that the cereal.txt contained information on 7000 cereals instead of around 70. Would searching for a specific cereal be more efficient if the records were ordered by cereal name? Why or why not? If we were interested in searching by name or by manufacturer, would ordering help? Explain.

3.  How fast was your program at reading and writing the files? Can you think of a way to reduce how long it takes to search a file?