/* * Reads standard input and outputs non-empty lines to standard output */ #ifndef __cplusplus #error Please use a C++ compiler to build this application #endif #include #include #include using namespace std; const string sSyntax = "Reads standard input and outputs the " "non-empty lines to standard output\n"; int main(int argc, const char *argv[]) { int iExitCode = EXIT_FAILURE; try { if (argc == 1) { string line; bool emptyLine = false; cin.exceptions(istream::failbit | istream::badbit); cout.exceptions(ostream::failbit | ostream::badbit | ostream::eofbit); cout.sync_with_stdio(false); // They say this might improve performace a bit while (!cin.eof()) { getline(cin, line); string::iterator lineEnd = line.end(), lineBegin = line.begin(); // Trime trailing spaces if (lineEnd != lineBegin) { do lineEnd--; while ( lineEnd != lineBegin && ( *lineEnd == ' ' || *lineEnd == '\r' || *lineEnd == '\n' || *lineEnd == '\t' || *lineEnd == '\f' || *lineEnd == '\v' ) ); if ( *lineEnd != ' ' && *lineEnd != '\r' && *lineEnd != '\n' && *lineEnd != '\t' && *lineEnd != '\f' && *lineEnd != '\v' ) lineEnd++; line.erase(lineEnd, line.end()); } if (line.empty()) if (emptyLine) ; // An empty line has already been output else { cout << endl; emptyLine = true; } else { cout << line; cout << endl; emptyLine = false; } } iExitCode = EXIT_SUCCESS; } else cout << sSyntax; } catch (ios_base::failure e) { cout << "Input/output error:\n"; cout << e.what(); } catch (exception e) { cout << "Error:\n"; cout << e.what(); } return iExitCode; }