# include # include # include # include using namespace std; int main ( int argc, char *argv[] ); int handle ( char file_in_name[], char file_out_name[] ); void timestamp ( void ); //****************************************************************************80 int main ( int argc, char *argv[] ) //****************************************************************************80 // // Purpose: // // CRRM removes carriage returns from a file. // // Usage: // // crrm // in which case the input and output files will be prompted for; // // crrm file1 // in which case the output file will be prompted for; // // crrm file1 file2 // // Modified: // // 09 January 2003 // // Author: // // John Burkardt // { char file_in_name[80]; char file_out_name[80]; char input[80]; int result; bool VERBOSE = false; if ( VERBOSE ) { timestamp ( ); cout << "\n"; cout << "CRRM:\n"; cout << " C++ version\n"; cout << " Replace carriage returns by line feeds.\n"; cout << "\n"; cout << " Compiled on " << __DATE__ << " at " << __TIME__ << ".\n"; } // // If the input file was not on the command line, get it now. // if ( argc < 2 ) { cout << "\n"; cout << "CRRM:\n"; cout << " Please enter the INPUT file name:\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 << "CRRM:\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] ); } // // Now we know the input and output file names, so go to it. // result = handle ( file_in_name, file_out_name ); if ( VERBOSE ) { cout << "\n"; cout << "CRRM:\n"; cout << " Normal end of execution.\n"; cout << "\n"; timestamp ( ); } return result; } //****************************************************************************80 int handle ( char file_in_name[], char file_out_name[] ) //****************************************************************************80 // // Purpose: // // HANDLE makes a copy of a file in which CR's are removed. // // Modified: // // 18 January 2003 // // 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. // // Output, int HANDLE, is 0 for success, or 1 if there was an error. // { char c; int cr_num; ifstream file_in; int file_in_char; ofstream file_out; int file_out_char; bool VERBOSE = false; // // Open the input and output files. // file_in.open ( file_in_name ); if ( !file_in ) { cout << "\n"; cout << "CRRM - Fatal error!\n"; cout << " Cannot open the input file " << file_in_name << ".\n"; return 1; } file_out.open ( file_out_name ); if ( !file_out ) { cout << "\n"; cout << "CRRM - Fatal error!\n"; cout << " Cannot open the output file " << file_out_name << ".\n"; return 1; } // // Transfer characters from the input file to the output file. // file_in_char = 0; file_out_char = 0; cr_num = 0; while ( 1 ) { file_in.get ( c ); if ( file_in.eof ( ) ) { break; } file_in_char = file_in_char + 1; if ( c == '\r' ) { cr_num = cr_num + 1; } else { file_out.put ( c ); file_out_char = file_out_char + 1; } } // // Close the files. // file_in.close ( ); file_out.close ( ); // // Report. // if ( VERBOSE ) { cout << "\n"; cout << " The input file contains " << file_in_char << " characters.\n"; cout << " The input file contains " << cr_num << " carriage returns.\n"; cout << " The output file contains " << file_out_char << " characters.\n"; } return 0; } //****************************************************************************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: // // 02 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 }