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

  1. /* strings.c  --  opens a file and searches it for */
  2. /*                possible strings                 */
  3.  
  4. #include <stdio.h>       /* for FILE, BUFSIZ & EOF */
  5. #include <ctype.h>       /* for isprint()          */
  6.  
  7. main(argc, argv)
  8. int argc;
  9. char *argv[];
  10. {
  11.     FILE *fp;
  12.     char buf[BUFSIZ];
  13.     int ch, count;
  14.  
  15.     if (argc != 2)
  16.         {
  17.         fprintf(stderr, "usage: strings file\n");
  18.         exit(1);
  19.         }
  20.     if ((fp = fopen(argv[1], "rb")) == NULL)
  21.         {
  22.         fprintf(stderr, "Can't open %s\n", argv[1]);
  23.         exit(1);
  24.         }
  25.  
  26.     count = 0;
  27.     while ((ch = fgetc(fp)) != EOF)
  28.         {
  29.         if (! isprint(ch) || count >= (BUFSIZ - 1))
  30.             {
  31.             if (count > 5)
  32.                 {
  33.                 buf[count] = 0;
  34.                 puts(buf);
  35.                 }
  36.             count = 0;
  37.             continue;
  38.             }
  39.         buf[count++] = ch;
  40.         }
  41. }
  42.