// Copyright 1999 // College of Computer Science // Northeastern University Boston MA 02115 // This software may be used for educational purposes as long as this copyright // notice is retained at the top of all files // Should this software be modified, the words "Modified from Original" must be // included as a comment below this notice // All publication rights are retained. This software or its documentation may // not be published in any media either in whole or in part. /////////////////////////////////////////////////////////////////////////////// // Shell.cpp // // The common start program for student and faculty development /////////////////////////////////////////////////////////////////////////////// // The standard include files that include traditional C and C++ headers and // many other Core Tools headers ... see CHeaders.h for additional details #include "IOTools.h" #include "Graphics.h" #include "Random.h" // Prototypes void GetInputFile(ifstream& InFile); void GetOutputFile(ofstream& OutFile); int main(int argc, char* argv[]) { // Use the following line if you choose NOT to open any graphics windows // InitializeConsole(); // Build graphics window 0 GraphicsWindow GW0(300, 300); // Move the console below graphics window 0 ConsolePlaceBelow(0); // Give the console the focus for user interaction MakeConsoleForeground(); ////////// // Enter the main program here //*** 1. opening a file in the same directory as the code using a fixed name // In is the file variable; "circledata.txt" is the name of the file on disk // Look at the file. Each line has 6 integers intended to be x, y, radius, R, G, B ifstream In("circledata.txt"); // input file stream int x, y, radius, R, G, B; while(In){ In >> x >> y >> radius >> R >> G >> B; // works just like cin SetForeColor(R, G, B); PaintCircle(x, y, radius); } //*** 2. You can also type in a file name // The function GetInputFile shows you how to do that. // Look at the code and explain it. // Here is some code that calls GetInputFile ifstream myFile; GetInputFile(myFile); // Read the file one char at a time and write it to cout // Run this program 3 times and try entering: // twinkle.txt // circledata.txt // Shell.cpp (Yes, the program can read itself!) char ch; while (myFile.get(ch)) cout << ch; // How do you count the number of characters in the file? //*** 3. Write to a file ofstream outFile; ifstream inFile; GetOutputFile(outFile); // Create an output file GetInputFile(inFile); // Open Input file, myFile // copy input file to output file while (inFile.get(ch)) outFile << ch; //*** 4. Change this code so that the new file will have a * wherever the old file had a blank ////////// // The lines below make sure that the graphics windows remain open just // before the program terminates PressReturn("\nThe main program is about to terminate\n"); return 0; } void GetInputFile(ifstream& InFile){ string InFileName; // Get file name cout << endl; RequestString("Enter input file name: ", InFileName); // If a name was entered, try to open the file if (InFileName.length() > 0){ InFile.open(InFileName.c_str()); if (!InFile){ cerr << "Cannot open " << InFileName << endl; exit(1); } } } void GetOutputFile(ofstream& OutFile){ string OutFileName; // Get file name cout << endl; RequestString("Enter output file name: ", OutFileName); // If a name was entered, try to open the file if (OutFileName.length() > 0){ OutFile.open(OutFileName.c_str()); if (!OutFile){ cerr << "Cannot open " << OutFileName << endl; exit(1); } } }