# include # include # include # include using namespace std; int main ( int argc, char *argv[] ); void handle ( char file_in_name[], char file_out_name[] ); int s_len_trim ( char *s ); void timestamp ( void ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // MAIN is the main program for DEBLANK. // // Discussion: // // DEBLANK makes a copy of a file, skipping all blank lines. // // A blank line is any line that contains no characters except blanks. // // Modified: // // 05 January 2005 // // Author: // // John Burkardt // { char file_in_name[80]; char file_out_name[80]; bool VERBOSE = false; if ( VERBOSE ) { timestamp ( ); cout << "\n"; cout << "DEBLANK:\n"; cout << " C++ version\n"; cout << "\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; cout << "\n"; cout << " Make a copy of a file, in which all blank lines are gone.\n"; } // // If the input file was not specified, get it now. // if ( argc <= 1 ) { cout << "\n"; cout << "DEBLANK:\n"; cout << " Please enter the name of the file to be deblanked.\n"; cin.getline ( file_in_name, sizeof ( file_in_name ) ); } else { strcpy ( file_in_name, argv[1] ); } // // If the output file was not on the command line, get it now. // if ( argc < 3 ) { cout << "\n"; cout << "DEBLANK:\n"; cout << " Please enter the OUTPUT file name:\n"; cin.getline ( file_out_name, sizeof ( file_out_name ) ); } else { strcpy ( file_out_name, argv[2] ); } handle ( file_in_name, file_out_name ); if ( VERBOSE ) { cout << "\n"; cout << "DEBLANK:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); } return 0; } //****************************************************************************80 void handle ( char file_in_name[], char file_out_name[] ) //****************************************************************************80 // // Purpose: // // HANDLE handles a single file. // // Discussion: // // The routine reads a line, and if it is not a blank line, copies // it to the output file. // // Modified: // // 05 January 2004 // // Author: // // John Burkardt // // Parameters: // // Input, char FILE_IN_NAME[], the name of the input file. // // Input, char FILE_OUT_NAME[], the name of the output file. // { int blank_count; ifstream file_in; ofstream file_out; char line[255]; // // Open the input file. // file_in.open ( file_in_name ); if ( !file_in ) { cout << "\n"; cout << "HANDLE - Fatal error!\n"; cout << " Cannot open \"" << file_in_name << "\"\n"; return; } // // Open the output file. // file_out.open ( file_out_name ); if ( !file_out ) { cout << "\n"; cout << "HANDLE - Fatal error!\n"; cout << " Cannot open \"" << file_out_name << "\"\n"; return; } blank_count = 0; while ( 1 ) { file_in.getline ( line, sizeof ( line ) ); if ( file_in.eof() ) { break; } if ( 0 < s_len_trim ( line ) ) { file_out << line << "\n"; } else { blank_count = blank_count + 1; } } // // Close the files. // file_in.close ( ); file_out.close ( ); cout << file_in_name << " contained " << blank_count << " blank lines.\n"; return; } //****************************************************************************80 int s_len_trim ( char *s ) //****************************************************************************80 // // Purpose: // // S_LEN_TRIM returns the length of a string to the last nonblank. // // Modified: // // 26 April 2003 // // Author: // // John Burkardt // // Parameters: // // Input, char *S, a pointer to a string. // // Output, int S_LEN_TRIM, the length of the string to the last nonblank. // If S_LEN_TRIM is 0, then the string is entirely blank. // { int n; char *t; n = strlen ( s ); t = s + strlen ( s ) - 1; while ( 0 < n ) { if ( *t != ' ' ) { return n; } t--; n--; } return n; } //****************************************************************************80 void timestamp ( void ) //****************************************************************************80 // // Purpose: // // TIMESTAMP prints the current YMDHMS date as a time stamp. // // Example: // // May 31 2001 09:45:54 AM // // Modified: // // 04 October 2003 // // Author: // // John Burkardt // // Parameters: // // None // { #define TIME_SIZE 40 static char time_buffer[TIME_SIZE]; const struct tm *tm; size_t len; time_t now; now = time ( NULL ); tm = localtime ( &now ); len = strftime ( time_buffer, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); cout << time_buffer << "\n"; return; #undef TIME_SIZE }