90 likes | 240 Vues
Some C++ I/O tricks. // Turn terminal I/O on for debug mode // comment out for file I/0 #define _debug_on ifstream inFile2; ofstream outFile2; #ifndef _debug_on #define cin inFile2 #define cout outFile2 #endif. Effect of compiler directive not redefining cin and cout. int main()
E N D
Some C++ I/O tricks // Turn terminal I/O on for debug mode // comment out for file I/0 #define _debug_on ifstream inFile2; ofstream outFile2; #ifndef _debug_on #define cin inFile2 #define cout outFile2 #endif
Effect of compiler directive not redefining cin and cout. int main() { inFile2.open("commands.txt"); outFile2.open("trace.txt"); cin >> currentCommand; cout << "Processing: " << currentCommand << endl; } Effect of compiler directive to define cin and cout as inFile2 and outFile2. int main() { inFile2.open("commands.txt"); outFile2.open("trace.txt"); inFile2 >> currentCommand; outFile2 << "Processing: " << currentCommand << endl; }
File Input void CorporateWorld::LoadCompanyNames(char fileName[]) { ifstream inFile; inFile.open(fileName); inFile >> cName; while (inFile) { < do some processing > inFile >> cName; } inFile.close(); }
cin >> currentCommand; cout << "Processing: " << currentCommand << endl; while (currentCommand.getEnumType() != END) { switch (currentCommand.getEnumType()) { case JOIN : cin >> person >> company; CW.JOIN(person, company); break; case QUIT : cin >> person; CW.QUIT(person); break; < additional cases added here > } cin >> currentCommand; cout << "Processing: " << currentCommand << endl; }
Define an enumerated type #ifndef ENUMCOMMANDTYPE_H #define ENUMCOMMANDTYPE_H enum CommandType { JOIN, QUIT, CHANGE, PROMOTE, DEMOTE, PAYDAY, EMPLOYEES, UNEMPLOYED, HISTORY, DUMP, END }; #endif
Convert a string to an enumerated type CommandType commandWord::getEnumType() { if (strcmp(cWord,"JOIN") == 0) return JOIN; else if (strcmp(cWord,"QUIT") == 0) return QUIT; else if (strcmp(cWord,"PROMOTE") == 0) return PROMOTE; else if (strcmp(cWord,"DEMOTE") == 0) return DEMOTE; else if (strcmp(cWord,"EMPLOYEES") == 0) return EMPLOYEES; else if (strcmp(cWord,"UNEMPLOYED") == 0) return UNEMPLOYED; else if (strcmp(cWord,"PAYDAY") == 0) return PAYDAY; else if (strcmp(cWord,"DUMP") == 0) return DUMP; else if (strcmp(cWord,"HISTORY") == 0) return HISTORY; else if (strcmp(cWord,"CHANGE") == 0) return CHANGE; else if (strcmp(cWord,"END") == 0) return END; else return (CommandType) -1; }
#ifndef COMMANDWORD_H #define COMMANDWORD_H #include <string.h> // Provides strcmp - string compare #include <stdlib.h> // Provides size_t typedef char commandStr[15]; class commandWord { public: commandWord(); CommandType getEnumType(); const char* getCommandStr() { return cWord; } friend istream& operator>>(istream&, commandWord&); friend ostream& operator<<(ostream&, const commandWord&); private: commandStr cWord; };
Defined in Implementation of commandWord class // friend functions for overloaded insertion and extraction operators istream& operator>> (istream& istr, commandWord& x) { istr >> x.cWord; return istr; } ostream& operator<< (ostream& ostr, const commandWord& x) { ostr << x.cWord; return ostr; }
Employee Record eName cName rank salaryYTD Employment History