[C] File I/O and params

February 7, 2010

Written to copy a large file across several floppy disks!: Simple example of file I/O, buffering, and input parameters. The pgm breaks a file into diskette (or any) size pieces. if the parameters are not correct, instructions are written to the screen.

 #include                          /*       BREAK UP . C       */
 #define  FALSE  -1                         /* +----------------------+ */
 #define  TRUE  0                           /* | This c program is to | */
 FILE   *input_file_pointer ;               /* | break a file up into | */
 FILE  *output_file_pointer ;               /* | floppy diskette size | */
 char   OutNames [16] ;                     /* | pieces.(or any size) | */
 int   indx = 1 ;                           /* +----------------------+ */
                                            /*  by Greg Smith;  1-31-89 */
 main ( int StrSegs, char *CmdLineStr [] )
    {                     
    long KiloBytes = 0 ;
    int  all_ok = TRUE ;   /*  the command format is:                   */
    long count ;           /*    BREAKUP     FILENAME.EXT  KILOBYTES    */
    int  askii ;           /*  thisprogram   parameter-1   parameter-2  */
    int  index ;     
                           /*  count is a long integer to accomodate    */
                           /*  files over 32k.  (and up to 2 gig.)      */

    /* --------------+----------------------------------+-------------- */
    /* --------------+    B E G I N   P R O G R A M:    +-------------- */
    /* --------------+----------------------------------+-------------- */

    if ( StrSegs < 2 )   /* THE 1ST PARAMATER IS THE NAME OF THIS PROG.*/
       {
       printf ("\n\t  1st param:  %s ",    CmdLineStr[1]  );
       printf ("\n\t  2nd param:  %s \n",  CmdLineStr[2]  );
  
       printf ("\n\t  This program is to break up a file into floppy" );
       printf ("\n\t  diskette size pieces.   The command is      \n" );
       printf ("\n\t  %s   filename.ext   KiloBytes\n",  CmdLineStr[0]  );
       printf ("\n\t  where KiloBytes = the number of  kb's  for each " );
       printf ("\n\t  diskette sized piece. (360, 1200, 1440 or etc.) " );
       printf ("\n\t  The  output filenames  will be  nnFILENAME.EXT  " );
       printf ("\n\t  where nn is a number:  01, 02, 03, ..., 99.     " );
       printf ("\n\t  The max.# of fragments is 99; size: 4 to 14400 kb's\n");
       all_ok = FALSE ;
       }
    if ( all_ok == TRUE )
       if ( ( input_file_pointer = fopen ( CmdLineStr[1], "rb") )  == NULL )
          {                     /* IF THE INPUT FILE CANNOT BE OPENED */
          printf ("\n input file  %s  cannot not be opened. \n",
                                  CmdLineStr[1] );
          all_ok = FALSE ;
          }
    if ( all_ok == TRUE )
       {       /*  see function  CkOutFile  below  */
       strcpy ( OutNames, CmdLineStr[1] ) ;
       strcat ( OutNames, "01" ) ;
       if ( ( output_file_pointer = fopen ( OutNames, "wb") )  == NULL )
          {                  /* IF THE OUTPUT FILE CANNOT BE OPENED */
          printf ("\n the 1st output file  %s  could not be opened. \n",
             OutNames );
          all_ok = FALSE ;
          }
         else
          {
          printf ( " Now  writing  file  # 1.  %s \n",  OutNames );
          }
       }
    if ( all_ok == TRUE )
       for ( index = 0 ; index < strlen( CmdLineStr[2] ) ; index++ )
          if ( CmdLineStr[2][index] < '0' || CmdLineStr[2][index] > '9' )
               all_ok = FALSE ; /* check input string for non-numerics */
       /*  end for  */
    if ( all_ok == TRUE )
       {
       KiloBytes = atoi ( CmdLineStr[2] ) ; /* Ascii TO Integer */
       if ( KiloBytes < 004  ||  KiloBytes > 14400 )  all_ok = FALSE ;
       }           /*   300  (004 is for testing )   */
    while ( all_ok == TRUE )  /*  BREAK UP THE FILE  */
       {
       for ( count = 001;  count <= KiloBytes*1000 ;  count++ )
          if ( (askii = getc (input_file_pointer) ) != EOF )
                      /* misspelled to avoid confusion.  The character 
                         must be converted to an  integer  to properly 
                         recognize the  e.o.f.  point in a binary file 
                         due to a carry over from the old CP/M op.sys.  */
             { putc (askii, output_file_pointer); }
            else {
             count = KiloBytes*1000 + 1 ;   /* set over limit to stop */
             all_ok = FALSE ;
             }
       /* end for */
       fclose ( output_file_pointer );
       if ( all_ok == TRUE )  CkOutFile(all_ok, CmdLineStr[1] ) ;
       } /* END WHILE */
       printf ("\n    use   COPY  ??FILENA.EXT /B  FILENAME.EXT   to ");
       printf       (" rebuild the file. \n");

    fclose ( input_file_pointer );
    } /* END  MAIN  PROGRAM */

       CkOutFile(int all_ok, char *StrSegmt[] )
          {
          static char *floppy[100] = { 
            "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", 
            "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
            "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
            "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
            "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
            "50", "51", "52", "53", "54", "55", "56", "57", "58", "59",
            "60", "61", "62", "63", "64", "65", "66", "67", "68", "69",
            "70", "71", "72", "73", "74", "75", "76", "77", "78", "79",
            "80", "81", "82", "83", "84", "85", "86", "87", "88", "89",
            "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", } ;

          all_ok = TRUE ;
          indx++ ;
          strcpy ( OutNames, StrSegmt ) ;
          strcat ( OutNames, floppy[indx] ) ;
          printf ( "\t\t     #%2d.  %s \n", indx, OutNames );
          if ( indx == 99 )  all_ok = FALSE ; 
          if ( ( output_file_pointer = fopen ( OutNames, "wb") )  == NULL )
             {                  /* IF THE OUTPUT FILE CANNOT BE OPENED */
             printf ("\n output file Number %d.  %s  could not be opened. \n",
                                           indx, OutNames );
             all_ok = FALSE ;
             }
          return all_ok ;
          } /*  END  ckOutFile  */

Comments are closed.

We try to post all comments within 1 business day