home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / qc_prog / chap10 / ccopy.c next >
Encoding:
C/C++ Source or Header  |  1988-04-06  |  1.3 KB  |  53 lines

  1. /* ccopy.c  --  Copies a file, cutting blank lines and  */
  2. /*              leading space from lines of copy        */
  3.  
  4. #include <stdio.h>        /* for FILE, BUFSIZ, NULL */
  5. #include <ctype.h>        /* for iswhite()          */
  6.  
  7. main(argc, argv)
  8. int argc;
  9. char *argv[];
  10. {
  11.     FILE *fp_in, *fp_out;
  12.     char buf[BUFSIZ];
  13.     char *cp;
  14.  
  15.     if (argc != 3)
  16.         {
  17.         printf("usage: ccopy infile outfile\n");
  18.         exit(1);
  19.         }
  20.     if ((fp_in = fopen(argv[1], "r")) == NULL)
  21.         {
  22.         printf("Can't open %s for reading.\n", argv[1]);
  23.         exit(1);
  24.         }
  25.     if ((fp_out = fopen(argv[2], "w")) == NULL)
  26.         {
  27.         printf("Can't open %s for writing.\n", argv[2]);
  28.         exit(1);
  29.         }
  30.  
  31.     printf("Copying and Crushing: %s->%s ...", 
  32.             argv[1], argv[2]);
  33.  
  34.     while (fgets(buf, BUFSIZ, fp_in) != NULL)
  35.         {
  36.         cp = buf;
  37.         if (*cp == '\n')    /* blank line */
  38.             continue;
  39.         while (isspace(*cp))
  40.             {
  41.             ++cp;
  42.             }
  43.         if (*cp == '\0')    /* empty line */
  44.             continue;
  45.         if (fputs(cp, fp_out) == EOF)
  46.             {
  47.             printf("\nError writing %s.\n", argv[2]);
  48.             exit(1);
  49.             }
  50.         }
  51.     printf("Done\n");
  52. }
  53.