home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / KILLFF.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  4KB  |  125 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. ** KILLFF.C - A program was written to strip out all the Form Feeds
  5. ** in text files.
  6. **
  7. ** Public domain by Erik VanRiper, 12/22/91
  8. ** Modified by Bob Stout, 17 Feb 93
  9. **
  10. ** Reads a text file and makes a duplicate with NO Form Feed
  11. ** characters! The default action is to create a duplicate without
  12. ** Form Feeds, then remove the original and rename the duplicate,
  13. ** although an explicit output file name may be specified.
  14. **
  15. ** Form Feed characters are replaced with newline characters ('\n').
  16. ** Since ANSI mandates that fwrite() will translate newlines when
  17. ** a stream is opened in text (non-binary) mode, these will appear
  18. ** in the output file in a format appropriate to the implementation,
  19. ** e.g. CRLF pairs on PC's.
  20. **
  21. ** Usage: KILLFF filename [newname]
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <stdlib.h>
  27.  
  28. #define BSIZ 32768U             /* max size of read/write buffer */
  29.  
  30. main(int argc, char *argv[])
  31. {
  32.    FILE *in, *out;              /* input and output files        */
  33.    char name[80],               /* name of file to be fixed      */
  34.         temp[80],               /* output file name              */
  35.         *buf;                   /* buffer we will use to write   */
  36.    size_t bad,                  /* check to see if write ok      */
  37.           num;                  /* number of bytes read          */
  38.    int retval  = EXIT_SUCCESS,  /* return value                  */
  39.        tmpflag = 0;             /* non-zero if tmpnam() used     */
  40.  
  41.    printf("\nKILL FORM FEEDS by Erik VanRiper & Bob Stout\n\n");
  42.  
  43.    if(argc < 2)                              /* usage info       */
  44.    {
  45.       puts("Usage: KILLFF input_file [output_file]");
  46.       puts("\nIf no output file is given, the input file will "
  47.            "be replaced.");
  48.       return retval;                         /* return to OS     */
  49.    }
  50.  
  51.    strcpy(name,argv[1]);                     /* input filename   */
  52.    if(argc == 3) strcpy(temp,argv[2]);       /* outfile name     */
  53.    else
  54.    {
  55.       tmpnam(temp);
  56.       tmpflag = -1;
  57.    }
  58.  
  59.    if((in = fopen(name,"r")) == NULL)        /* Open in file     */
  60.    {
  61.       printf("\nCan't Open Input File %s",name);
  62.       return (retval = EXIT_FAILURE);        /* return to OS     */
  63.    }
  64.    if((out = fopen(temp,"wt")) == NULL)      /* open out file    */
  65.    {
  66.       printf("\nCan't Open Output File %s",temp);
  67.       fclose(in);                            /* close in file    */
  68.       return (retval = EXIT_FAILURE);        /* return to OS     */
  69.    }
  70.  
  71.    if((buf = malloc(BSIZ)) == NULL)     /* malloc a large buffer */
  72.    {
  73.       printf("\nOut of memory\n");
  74.       return (retval = EXIT_FAILURE);        /* return to OS     */
  75.    }
  76.  
  77.    printf("Input file: %s Output file: %s\n",
  78.       name,tmpflag ? name : temp);
  79.  
  80.    /* read in file while chars to read */
  81.  
  82.    while (0 < (num = fread(buf,sizeof(char),BSIZ,in)))
  83.    {
  84.       size_t i;
  85.  
  86.       for (i = 0; i < num; ++i)              /* look for FF      */
  87.          if ('\f' == buf[i])
  88.             buf[i] = '\n';                  /* change to newline */
  89.  
  90.       bad=fwrite(buf,sizeof(char),num,out);  /* write out buf    */
  91.       if(bad != num)                         /* error            */
  92.       {
  93.          printf("\nCan't Write to %s ", temp);
  94.          retval = EXIT_FAILURE;              /* return to OS     */
  95.          break;
  96.       }
  97.    }
  98.    fclose(in);                               /* close in file    */
  99.    fclose(out);                              /* close out file   */
  100.    free(buf);                                /* free memory      */
  101.    if (tmpflag)
  102.    {
  103.       if (remove(name))
  104.       {
  105.          printf("Can't rename %s\n", name);
  106.          printf("Converted file is named %s\n", temp);
  107.       }
  108.       else
  109.          rename(temp, name);
  110.    }
  111.    printf("\nDone!");                        /* Finished         */
  112.    return retval;                            /* return to OS     */
  113. }
  114. /*
  115.  
  116. List this source file to test this program!
  117.  
  118. New page
  119.  
  120. New page
  121.  
  122. All done
  123.  
  124. */
  125.