home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / mslang / cp1 / str1.c! < prev    next >
Text File  |  1993-04-09  |  3KB  |  86 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville, MI
  3. Date: 03-28-93 (17:22)             Number: 48
  4. From: BOB STOUT                    Refer#: 45
  5.   To: FRED COLE                     Recvd: NO  
  6. Subj: FINDING FILE LENGTH            Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. In a message of <Mar 26 20:09>, Fred Cole (1:231/50.4@fidonet.org) writes:
  9.  
  10.  
  11.  >I have a nasty habit of concatenating to the end of files and not paying
  12.  >attention to any ^Z chars that may be lurking about.  Some editors are VERY
  13.  >unforgiving and I don't always remember to check before I save a file. Thank
  14.  >God for Peter Norton!  May he live long and prosper. <g>
  15.  
  16.   No need, use this from the next SNIPPETS:
  17.  
  18. /*
  19. **  STRIPEOF.C
  20. **
  21. **  public domain demo by Bob Stout
  22. */
  23.  
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <io.h>
  27. #include <fcntl.h>
  28.  
  29. #define BUFSIZE 16384
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.       char *buf;
  34.  
  35.       if (2 > argc)
  36.       {
  37.             puts("Usage: STRIPEOF filename1 [...filenameN]");
  38.             return EXIT_FAILURE;
  39.       }
  40.       if (NULL == (buf = malloc(BUFSIZE)))
  41.       {
  42.             puts("STRIPEOF internal failure");
  43.             return EXIT_FAILURE;
  44.       }
  45.       while (--argc)
  46.       {
  47.             int fd;
  48.             size_t bytes;
  49.             int found = 0;
  50.             long zpos = 0L;
  51.  
  52.             if (-1 == (fd = open(*(++argv), O_RDWR | O_BINARY)))
  53.             {
  54.                   printf("Couldn't open %s\n", *argv);
  55.                   return EXIT_FAILURE;
  56.             }
  57.             while (0 < (bytes = read(fd, buf, BUFSIZE)))
  58.             {
  59.                   int i;
  60.  
  61.                   for (i = 0; i < (int)bytes; ++i)
  62.                   {
  63.                         if (('Z' - 64) == buf[i])
  64.                         {
  65.                               found = 1;
  66.                               zpos += i;
  67.                               break;
  68.                         }
  69.                   }
  70.                   if (found)
  71.                         break;
  72.                   zpos += bytes;
  73.             }
  74.             if (found)
  75.                   chsize(fd, zpos);
  76.       }
  77.       return EXIT_SUCCESS;
  78. }
  79.  
  80.  
  81. --- QM v1.00
  82.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  83. SEEN-BY: 1/211 11/2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19 124/1
  84. SEEN-BY: 153/752 154/40 77 157/2 159/100 125 575 950 203/23 209/209 280/1
  85. SEEN-BY: 390/1 396/1 5 15 2270/1 3603/20
  86.