CSCI 261 - Programming Concepts (C++)Fall 2016 - Lab 09AQuick Links: Blackboard | EECS | Mines | Piazza | zyBooks |
|
| Home | Contact | Syllabus | Assignments | Schedule | Resources | | |
This lab is due by November 02, 2016 08:00am.
ConceptsThe 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 FileIn 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.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
Remember Lab06B where we created a simple Turtle Graphics program that printed out screen coordinates to
our terminal? As an intermediate step towards Assignment 09, we are going to save our Turtle's location
to an external file in addition to printing them to the terminal. You will probably want to use your solution to Lab06B
as the starting part for this lab.
As before, first ask the user for the resolution of our circle appoximation. Print out the theta and distance that will be used. Now ask the user to enter a string for the filename that we want to save our points to. We now need to open an output file stream that will save data to the corresponding filename. Before our program printed the coordinates to the terminal, now we want to print the coordinates to an external file. The file should have the following format; # of points Point1X Point1Y Point2X Point2Y Point3X Point3Y etc...
That is, the first line should be the number of points we will print. Then each following line will be the X and Y location of each
point, separated by a space.
Since we were using a recursive function previously to generate and print our points, we will want to pass our output file stream as an argument to the function. This is simple to do as it is just another variable. One thing to note, file streams may ONLY be passed by REFERENCE. As an example, the function below may be helpful (you might want to modify it, create different versions that accept different parameter types, etc.). void PrintToFile( ofstream &out, const string txt ) { out << txt; } int main() { ofstream outFile; // open outFile properly, etc. PrintToFile( outFile, "Hello World!" ); return 0; }
A sample program output is below:
Enter the resolution to approximate the circle to: 6 Theta = 1.047 Dist = 1.000 Enter a filename to save the points to: circle_6.txt Saving points to file "circle_6.txt"... ...writing number of points 6 to file... ...writing point (1.500, 0.866) to file... ...writing point (1.000, 1.732) to file... ...writing point (0.000, 1.732) to file... ...writing point (-0.500, 0.866) to file... ...writing point (0.000, 0.000) to file... ...writing point (1.000, 0.000) to file... ...finished writing points!
Our file should then have the following contents (based on the program above):
6 1.500 0.866 1.000 1.732 0.000 1.732 -0.500 0.866 0.000 0.000 1.000 0.000
This file will now serve as the input to Assignment 09.
Lab Submission
You will submit your solution to this lab with the rest of Week09. Detailed instructions for doing this are posted in Assignment 09.
This lab is due by November 02, 2016 08:00am.
| |
Last Updated: 01/01/70 00:00
|