Part I

Use Notepad.exe to create a short file with a paragraph of text. Enter something similar to the following:

My name is Mark Bowman.

I like to play volleyball and frisbee.

I teach at LCC and work at MSU.

Save the file as C:\Lab08.txt. Create a workspace and enter the program below. Compile and run the program, using the name of your text file as the input.

/*****************************************

* Lab 08

* Written by YOUR NAME HERE

* 09-Apr-12

****************************************/

#include <iostream>

#include <fstream>

#include <string>

using namespace std;

// Main Routine

void main()
{ int count;

string s;

fstream f;

// Initialize

count = 0;

// Open file

cout < "Enter file name: ";

cin > s;

f.open(s.data(),ios::in);

// Loop through file

while(!f.eof())

{ f > s;

// If it's okay, show it

if(f.good())

{ count++;

cout < s;

};

};

// Close file

f.close();

// Output # of chars, words, lines

cout < "\nCount: " < count < endl;

}


1. How many words are in your text file?

2. Add a character variable c to your program. Modify the input loop so that it uses:

f > c;

to read characters from the file.

Compile and run the program again. How many visible characters are in your text file?

3. Change the input loop again, so that it now uses the get() function to read individual characters from the file.

Compile and run the program. How many total characters are in your text file?

4. One more time, change the input loop so that it uses the getline() function to read lines from the file.

Compile and run the program. How many lines are in your text file?

5. Of the steps above, which outputs resulted in a display closest to how your file is displayed in Notepad.exe?

Part II

Write a program that reads values from an input file and stores the positive numbers into an output file. The user should be prompted for the names of the input and output file. The input file will contain a 0 value to indicate the last number in the file to be read.

Sample Run / In.txt / Out.txt
Positive Filter Program
Input file name: C:\IN.TXT
Output file name: C:\OUT.TXT
Finished!
Press any key to continue. / 5
17
-3 2 1
12
9 8
-17
0 4
-4
27 / 5
17
2
1
12
9
8

You will need to use Notepad.exe to create the input file. Call it whatever you want, and store it someplace easy to type in when you run your program. After running the program, check that the output file is correct using Notepad.exe.

A Note On File Names

To access files from our C++ programs, you will often have to include the location of the file with it’s name. Here are some examples:


A:\Blob.cpp /
D:\230\Lab08\Debug\Lab08.exe

The location, or path to the file, is put in front of the file name. Windows likes to hide file extensions (.cpp, .exe, .txt). The file extension must be used when entering a file name.

What To Hand In

Part I: A copy of your input file, and your answers to the questions.

Part II: A copy of your program, the run-time output, the input file, and the output file.