some c i o tricks n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Some C++ I/O tricks PowerPoint Presentation
Download Presentation
Some C++ I/O tricks

Loading in 2 Seconds...

play fullscreen
1 / 9

Some C++ I/O tricks - PowerPoint PPT Presentation


  • 106 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Some C++ I/O tricks' - mabli


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
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

slide2

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
File Input

void CorporateWorld::LoadCompanyNames(char fileName[])

{

ifstream inFile;

inFile.open(fileName);

inFile >> cName;

while (inFile)

{

< do some processing >

inFile >> cName;

}

inFile.close();

}

slide4

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;

}

slide5

Define an enumerated type

#ifndef ENUMCOMMANDTYPE_H

#define ENUMCOMMANDTYPE_H

enum CommandType { JOIN, QUIT, CHANGE, PROMOTE,

DEMOTE, PAYDAY, EMPLOYEES,

UNEMPLOYED, HISTORY, DUMP,

END };

#endif

slide6

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;

}

slide7

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

};

slide8

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;

}

slide9

Employee Record

eName

cName

rank

salaryYTD

Employment

History