1 / 9

Some C++ I/O tricks

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()

mabli
Download Presentation

Some C++ I/O tricks

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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; }

  3. File Input void CorporateWorld::LoadCompanyNames(char fileName[]) { ifstream inFile; inFile.open(fileName); inFile >> cName; while (inFile) { < do some processing > inFile >> cName; } inFile.close(); }

  4. 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; }

  5. Define an enumerated type #ifndef ENUMCOMMANDTYPE_H #define ENUMCOMMANDTYPE_H enum CommandType { JOIN, QUIT, CHANGE, PROMOTE, DEMOTE, PAYDAY, EMPLOYEES, UNEMPLOYED, HISTORY, DUMP, END }; #endif

  6. 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; }

  7. #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; };

  8. 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; }

  9. Employee Record eName cName rank salaryYTD Employment History

More Related