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

  1. /* upity.c --  makes an uppercase copy of a file using */
  2. /*             fread() and fwrite()                    */
  3.  
  4. #include <string.h>        /* for strrchr() */
  5. #include <stdio.h>         /* for NULL      */
  6. #include <malloc.h>        /* for malloc()  */
  7. #include <ctype.h>         /* for isupper() */
  8.  
  9. #define HUNK 512
  10.  
  11. main(argc, argv)
  12. int argc;
  13. char *argv[];
  14. {
  15.     char *cp, newname[128], *np;
  16.     FILE *fp;
  17.     int  hunks = 0, bytes = 0, totbytes = 0;
  18.     int  i;
  19.     if (argc != 2)
  20.         {
  21.         printf("usage: upity file\n");
  22.         exit(1);
  23.         }
  24.  
  25.     if ((fp = fopen(argv[1], "rb")) == NULL)
  26.         {
  27.         printf("\"%s\": Can't open.\n", argv[1]);
  28.         exit(1);
  29.         }
  30.     if ((cp = malloc(HUNK)) == NULL)
  31.         {
  32.         printf("Malloc Failed.\n");
  33.         exit(1);
  34.         }
  35.  
  36.     while ((bytes = fread(cp + (HUNK * hunks), 1, HUNK, fp)) == HUNK)
  37.         {
  38.         totbytes += bytes;
  39.         ++hunks;
  40.         if ((cp = realloc(cp, HUNK + (HUNK * hunks))) == NULL)
  41.             {
  42.             printf("Realloc Failed.\n");
  43.             exit(1);
  44.             }
  45.         }
  46.     if (bytes < 0)
  47.         {
  48.         printf("\"%s\": Error Reading.\n", argv[1]);
  49.         exit(1);
  50.         }
  51.     totbytes += bytes;
  52.  
  53.     for (i = 0; i < totbytes; ++i)
  54.         if (islower(cp[i]))
  55.             cp[i] = toupper(cp[i]);
  56.     
  57.     (void)fclose(fp);
  58.  
  59.     if ((np = strrchr(argv[1], '.')) != NULL)
  60.         *np = '\0';
  61.     strcpy(newname, argv[1]);
  62.     strcat(newname, ".up");
  63.     if ((fp = fopen(newname, "wb")) == NULL)
  64.         {
  65.         printf("\"%s\": Can't open.\n", argv[1]);
  66.         exit(1);
  67.         }
  68.  
  69.     if (fwrite(cp, 1, totbytes, fp) != totbytes)
  70.         {
  71.         printf("\"%s\": Error writing.\n", argv[1]);
  72.         exit(1);
  73.         }
  74.  
  75. }
  76.