#include #include #include #include using namespace std; void DoCat(istream & in, ostream & out, bool asint); void Usage(string progname) { cout << progname << ":" << endl; cout << "\t --infile filename: specify the input file name" << endl; cout << "\t -i filename: specify the input file name" << endl; cout << "\t --outfile filename: specify the output file name" << endl; cout << "\t -o filename: specify the output file name" << endl; cout << "\t --help, ? : help" << endl; cout << endl; return ; } int main(int argc, char * argv[]){ bool readFromStdin{true}; bool writeToStdout{true}; ifstream inFile; ofstream outFile; istream * in = & cin; ostream * out = & cout; bool intDump{false}; string inFileName, outFileName; int i; i = 1; while (i < argc) { if ( !strcmp(argv[i],"--infile") or !strcmp(argv[i], "-i")) { ++i; if (i < argc) { inFileName = argv[i]; ++i; readFromStdin = false; } else { cerr << "Error: no input file name give" << endl; Usage(argv[0]); } } else if ( !strcmp(argv[i],"--outfile") or !strcmp(argv[i], "-o")) { ++i; if (i < argc) { outFileName = argv[i]; ++i; writeToStdout = false; } else { cerr << "Error: no output file name give" << endl; Usage(argv[0]); } } else if ( !strcmp(argv[i],"--help") or !strcmp(argv[i], "?")) { Usage(argv[0]); ++i; } else if ( !strcmp(argv[i],"--int") or !strcmp(argv[i], "-d")) { intDump = true; ++i; } else { cerr << "Unknown argument: " << argv[i] << endl; Usage(argv[0]); ++i; } } if (!readFromStdin) { inFile.open(inFileName); readFromStdin = !inFile; in = & inFile; } if (!writeToStdout) { outFile.open(outFileName); writeToStdout = !outFile; out = & outFile; } DoCat(*in, *out, intDump); return 0; } void DoCat(istream & in, ostream & out, bool intDump){ char letter; letter = in.get(); while(in) { if (intDump) { out << static_cast(letter); } else { out << letter; } letter = in.get(); } }