// Purpose: Read words and write in reverse order. A word contains only alphabetic characters. // // Command syntax: // $ ./a.out [ input-file [ output-file ] ] // all parameters are optional. Defaults: input-file => cin, output-file => cout // // Examples: // $ ./a.out // $ ./a.out inputfile // $ ./a.out inputfile outputfile #include #include #include #include #include #include #include // exit using namespace std; // direct access to std bool convert( int &val, char *buffer ) { // convert C string to integer std::stringstream ss( buffer ); // connect stream and buffer string temp; ss >> dec >> val; // convert integer from buffer return ! ss.fail() && // conversion successful ? ! ( ss >> temp ); // characters after conversion all blank ? } // convert int main( int argc, char *argv[] ) { istream *infile = &cin; // default value ostream *outfile = &cout; // default value infile->exceptions( ios_base::failbit ); // set cin/cout to use exceptions outfile->exceptions( ios_base::failbit ); try { switch ( argc ) { case 3: try { outfile = new ofstream( argv[2] ); // open output file outfile->exceptions( ios_base::failbit ); // set exceptions } catch( ios_base::failure ) { throw ios_base::failure( "could not open output file" ); } // try // FALL THROUGH case 2: try { infile = new ifstream( argv[1] ); // open input file infile->exceptions( ios_base::failbit ); // set exceptions } catch( ios_base::failure ) { throw ios_base::failure( "could not open input file" ); } // try // FALL THROUGH case 1: // all defaults break; default: // wrong number of options throw ios_base::failure( "wrong number of arguments" ); } // switch } catch( ios_base::failure err ) { cerr << err.what() << endl; cerr << "Usage: " << argv[0] << " [ input-file [ output-file ] ]" << endl; exit( EXIT_FAILURE ); // TERMINATE } // try string line, alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; list words; // list of words in document *infile >> noskipws; // turn off white space skipping during input try { for ( ;; ) { // process lines from input getline( *infile, line ); // read entire line, but not newline line += "\n"; // add newline as sentinel for ( ;; ) { // scan words in line string::size_type posn = line.find_first_of(alpha); // find position of 1st alphabetic character if ( posn == string::npos ) break; // any characters left ? line = line.substr( posn ); // remove leading whitespace posn = line.find_first_not_of(alpha); // find position of 1st non-alphabetic character string word = line.substr( 0, posn ); // extract word from start of line words.push_back( word ); // add word to end of list line = line.substr( posn ); // delete word from line } // for } // for } catch( ios_base::failure ) {} // end of file *outfile << "Words in reverse order:" << endl; // traverse list in reverse for ( list::reverse_iterator iter = words.rbegin(); iter != words.rend(); iter++ ) { cout << *iter << endl; // print word } // for if ( infile != &cin ) delete infile; // close file, do not delete cin! if ( outfile != &cout ) delete outfile; // close file, do not delete cout! } // main