#include #include #include #include using namespace std; char getAnswer(string OK) {char choice; do { cin.get(choice); choice=toupper(choice); } while (OK.find(choice)<0); return(choice); } void OutputDirection(ofstream &Dest) {string fileName; int done; do { done=1; // Assume ok. Will be ok unless a device open fails cout << "Output Destination:\nS)creen P)rinter D)isk\n"; cout.flush(); switch (getAnswer("SPD")) { case 'S': Dest.open("/dev/console",ios::app); if (Dest.fail()) { cout << "Attempt to Attach to Screen Failed\n"; done=0; Dest.close(); } break; case 'P': Dest.open("prn"); if (Dest.fail()) { cout << "Attempt to Attach to Printer Failed...Is Printer On Line?\n"; done=0; Dest.close(); } break; case 'D': cin.ignore(); cout << "Enter Filename to Open >"; cin >> fileName; cin.ignore(5,'\n'); // Try to open for read to see if it's already there ifstream temp(fileName.c_str()); if (temp) { cout << "File Exists! Overwrite? Y/N >"; if (getAnswer("YN")=='N') { done=0; } } if (done) { // If we can create destination Dest.open(fileName.c_str(),ios::out); if (Dest.fail()) { cout << "Attempt to Open " << fileName << " Failed\n"; done=0; Dest.close(); } } // if done } } while (!done); } // Open a file for reading. If file not opened, return -1, else return 0 int OpenForRead(ifstream &Source) {string filename; cout << "Enter File to Open >"; cin >> filename; cin.ignore(); // eat CR Source.open(filename.c_str()); if (Source.fail()) return(-1); return(0); } // Input a string from a stream, terminating when \n // is encountered. The terminator \n is discarded. void fgetline(ifstream &Incoming,char Str[]) {int i=0; char Dummy; do { Incoming.get(Str[i]); i++; } while (Str[i-1] != '\n' && !Incoming.eof()); Str[i-1]='\0'; }