/* @NTAF @(#) stream2record.c 1.1@(#) 92/09/16 /usr2/ntaf/test/sccs/c/s.stream2record.c Project : New Technology Assesment and Forcast System ( NTAF ) Program : stream2record Purpose : Convert byte stream file into records of a fixed length. Author : C. J. Hornback Power Computing Company 9401 Lee Highway, Fairfax VA Usage : stream2record stream_file record_file [ rec_size ] Modification History: 92-07-16: cjh: Program created to convert APS text input stream. */ #include FILE *apsinp ; FILE *apsotp ; int next_char ; int count ; long cnt_char = 0 ; long cnt_rec = 0 ; char record_file_name[ 80 ] ; int rec_size = 80 ; char stream_file_name[ 80 ] ; main( argc, argv ) int argc ; char *argv[] ; { if ( argc < 3 || argc > 4 ) { printf( "\n USAGE: %s stream_file record_file [rec_size]\n" , argv[ 0 ] ) ; exit( 1 ) ; } strcpy( stream_file_name, argv[ 1 ] ) ; strcpy( record_file_name, argv[ 2 ] ) ; if ( ( apsinp = fopen( stream_file_name, "r+" ) ) == NULL ) { printf( "\n Unable to open '%s' om READ mode.\n", stream_file_name ) ; exit( 1 ) ; } if ( ( apsotp = fopen( record_file_name, "w+" ) ) == NULL ) { printf( "\n Unable to open '%s' in WRITE mode.\n" , record_file_name ) ; exit( 1 ) ; } if ( argc == 4 ) { rec_size = atoi( argv[ 3 ] ) ; if ( rec_size < 1 ) rec_size = 80 ; } count = 0 ; while( !feof( apsinp ) ) { next_char = getc( apsinp ) ; if ( feof( apsinp ) ) break ; putc( next_char, apsotp ) ; cnt_char ++ ; count ++ ; if ( count == rec_size ) { putc( '\n', apsotp ) ; count = 0 ; cnt_rec ++ ; } if ( ferror( apsotp ) ) { printf( "\n ***** Write error at character %ld, record %ld\n" , cnt_char, cnt_rec ) ; exit( 2 ) ; } } if ( cnt_char % rec_size != 0 ) { putc( '\n', apsotp ) ; cnt_rec ++ ; } fclose( apsinp ) ; fclose( apsotp ) ; printf( " %ld characters read; %ld records written.\n" , cnt_char, cnt_rec ) ; exit( 0 ) ; }