home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / R_LA4_02.ZIP / FSEARCH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-01-13  |  1.3 KB  |  52 lines

  1. /* FSEARCH.C - From page 464 of "Microsoft C Programming for    */
  2. /* the IBM" by Robert Lafore. Searches a file for a phrase.     */
  3. /* Usage:  A>fsearch filename.ext phrase                        */
  4. /****************************************************************/
  5.  
  6. #include "fcntl.h"            /*needed for oflags*/
  7. #include "stdio.h"            /*needed for NULL etc.*/
  8. #include "memory.h"           /*needed for memchr();*/
  9. #define BUFFSIZE 1024
  10. char buff[BUFFSIZE];
  11.  
  12. main(argc, argv)
  13. int argc;
  14. char *argv[];
  15. {
  16. int inhandle, bytes;
  17.  
  18.    if(argc != 3) {
  19.       printf("\nFormat:  A>fsearch filename.ext phrase");
  20.       exit();
  21.    }
  22.    if((inhandle = open(argv[1], O_RDONLY)) < 0) {
  23.       printf("\nCan't open file %s.", argv[1]);
  24.       exit();
  25.    }
  26.    while((bytes = read(inhandle, buff, BUFFSIZE)) > 0)
  27.       search(argv[2], bytes);
  28.    close(inhandle);
  29.    printf("\nPhrase not found");
  30. }
  31.  
  32. /* search() */    /* searches buffer for phrase */
  33. search(phrase, buflen)
  34. char *phrase;
  35. int buflen;
  36. {
  37. char *ptr, *p;
  38.  
  39.    ptr = buff;
  40.    while((ptr = memchr(ptr, phrase[0], buflen)) != NULL)
  41.       if(memcmp(ptr, phrase, strlen(phrase)) == 0) {
  42.          printf("\nFirst occurrence of phrase:\n\n");
  43.          for(p = ptr - 100; p < ptr + 100; p++)
  44.             putchar(*p);
  45.          exit();
  46.       }
  47.       else ptr++;
  48. }
  49.  
  50.  
  51.  
  52.