/* Filename: maketext.cpp Author: Br. David Carlson Date: February 1, 1998 Last Revised: February 21, 2013 This program prompts the user for the name of a file to create. Then it asks the user to enter lines of text, which are written to that file. CTRL z is used to stop data entry. (Substitute CTRL d in Linux.) Tested with: Microsoft Visual C++ 2008 g++ under Linux */ #include #include // needed for files using namespace std; const int MaxString = 120; typedef char StringType[MaxString]; void WriteToFile(fstream & OutFile); int main(void) { StringType FileName; fstream OutFile; cout << "Enter the name of the text file to be created: "; cin.getline(FileName, MaxString); OutFile.open(FileName, ios::out); if (OutFile.fail()) { cerr << "Could not create file " << FileName << endl; exit(1); } WriteToFile(OutFile); OutFile.close(); // very important to close an output file return 0; } /* Given: OutFile A file stream already opened for output. Task: To get lines of text from the user and write them to OutFile. Return: OutFile The modified file stream. */ void WriteToFile(fstream & OutFile) { StringType Line; // change to CTRL d for Linux: cout << "Enter a line of text (or press CTRL z to quit):" << endl; cin.getline(Line, MaxString); while (! cin.fail()) { OutFile << Line << endl; // change to CTRL d for Linux: cout << "Enter a line of text (or press CTRL z to quit):" << endl; cin.getline(Line, MaxString); } }