Advertisement
userxbw

Search file for matching word C

Aug 21st, 2022 (edited)
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <unistd.h>
  5. #include <getopt.h>
  6.  
  7. char *readIntoBuffer(FILE *fp);
  8. FILE * openfile(char *file, int n);
  9. void printFile(char *f,int n);
  10. void seekWord(char *w,char *sword,
  11. FILE *storage);
  12. void usage(char *argv[]);
  13. void feedback(int c,int flag1,int flag2,
  14.               char * file, char * searchword,
  15.               int optopt,char * optarg);
  16.  
  17. void chopToChars(char *source,char *searchwd,
  18. FILE *resultptr);
  19.  
  20.  
  21. int main(int argc, char **argv){
  22.     int c,index,flag1,flag2,flag3,flag4,flag5;
  23.     char *file,*searchword;
  24.     opterr=0;
  25.  
  26.    /*
  27.    getopt options
  28.    placing the colon : to the right
  29.    of the letter in string_options
  30.    indecates required argument,
  31.    using two :: is optional argument
  32.   */
  33.    static char string_options[]=
  34.    "xpc::f:s:";
  35.  
  36.    /* longname
  37.       0  no_argument
  38.       1  required_argument
  39.       2  optional_argument
  40.       */
  41.    static struct option long_options[]=
  42.    {
  43.  
  44.      {"filename",required_argument,NULL,'f'},
  45.      {"searchword",required_argument,NULL,'s'},
  46.      {"print",no_argument,NULL,'p'},/*result file*/
  47.      {"printsource",no_argument,0,'x'}, /* print source file */
  48.      {"chop",0,0,'c'},/* search char by char */
  49.      {0,0,0,0}
  50.     // has proper formatting
  51.  //   {"f",1,0,100}, /* filename */
  52.  //   {"s",1,0,101}, /*search word */
  53.  //   {"p",0,0,102}, /*print result file */
  54.  //   {0,0,0,0}
  55.  
  56.  };
  57.  
  58.   while((c=getopt_long(argc,argv,string_options,
  59.          long_options,&index))!=-1)
  60.   {
  61.    switch(c)
  62.     {
  63.      case 'p':
  64.         flag3=1;
  65.         break;
  66.     case 'f': /* requiers argument so
  67.                  if its missing or
  68.                  another option - */
  69.       if( (optarg[0] == '-' ) ||
  70.            optarg == NULL){
  71.            usage(argv);
  72.       }
  73.       file=strdup(optarg);
  74.       flag1=1;
  75.       break;
  76.     case 's':
  77.        if( (optarg[0] == '-') ||
  78.            optarg == NULL){
  79.            usage(argv);
  80.       }
  81.       searchword=strdup(optarg);
  82.       flag2=1;
  83.       break;
  84.     case 'x':
  85.          flag4=1;
  86.          break;
  87.     case 'c':
  88.        flag5=1;
  89.        break;
  90.   case ':':
  91.        printf("optopt= %c optarg= %s\n",
  92.        optopt,optarg);
  93.        usage(argv);
  94.       break;
  95.    case '?':
  96.       if(optopt == 'f' ||
  97.          optopt == 's'){
  98.         fprintf(stderr, "Option -%c requires an "
  99.          "argument.\n",optopt);
  100.         usage(argv);
  101.           exit(1);
  102.          }
  103.       else if (isprint(optopt))
  104.         fprintf(stderr,"Unknown option -%c"
  105.         "\n",optopt);
  106.     else
  107.       fprintf(stderr,"Unknown option "
  108.             "character `\\x%x`.\n",
  109.             optopt);
  110.     break;
  111.     default:
  112.     break;
  113.     } // end switch
  114.   }//end while
  115.  
  116. //feedback(c,flag1,flag2,file,searchword,optopt,optarg);
  117.  
  118. if( !flag1 || !flag2 )
  119.   usage(argv);
  120.  
  121. /* source file */
  122. FILE *fptr = openfile(file,1);
  123. /* file for, if search word match
  124.    write search word to file
  125. */
  126. FILE *foundptr = openfile("found.txt",2);
  127.  
  128. /* source file */
  129. char *s=readIntoBuffer(fptr);
  130.  
  131. if(flag4)
  132. /* prints source file -x */
  133. printFile(s,1);
  134.  
  135.  
  136.  chopToChars(s,searchword,foundptr);
  137.  
  138. //seekWord(s,searchword,foundptr);
  139.  
  140. /*check found words read from file */
  141. char *fw=readIntoBuffer(foundptr);
  142. if(flag3)
  143. /* prints results file -p */
  144. printFile(fw,2);
  145. /* search char by char*/
  146.  
  147. //if(flag5)
  148.  //chopToChars(s,searchword,foundptr);
  149.  
  150. free(s);
  151. free(fw);
  152. fclose(fptr);
  153. fclose(foundptr);
  154. return 0;
  155. }
  156. FILE * openfile(char *file, int n) {
  157. FILE *fptr;
  158. if(n==2)
  159. /* wipes the file clean w+ */
  160. fptr = fopen(file,"w+");
  161. else
  162. fptr = fopen(file,"r");
  163.  
  164. if(fptr==NULL){
  165. printf("FILE %d B NULL\n",n);
  166. exit(1);
  167. }
  168. return fptr;
  169. }
  170.  
  171.  
  172. char *readIntoBuffer(FILE *fp){
  173. // unknown file size
  174. char *source = NULL;
  175.     /* make sure its at the start */
  176.     fseek(fp,0,SEEK_SET);
  177.     /* Go to the end of the file. */
  178.     if (fseek(fp, 0L, SEEK_END) == 0) {
  179.         /* Get the size of the file. */
  180.         long bufsize = ftell(fp);
  181.         if (bufsize == -1) { /* Error */ }
  182.  
  183.     /* Allocate our buffer to that size. */
  184.       source =
  185.       malloc(sizeof(char) * (bufsize + 1));
  186.  
  187.     /* Go back to the start of the file. */
  188.       if (fseek(fp, 0L, SEEK_SET) != 0)
  189.     { /* Error */ }
  190.  
  191.    /* Read the entire file into memory. */
  192.       size_t newLen =
  193.        fread(source, sizeof(char),
  194.        bufsize, fp);
  195.         if ( ferror( fp ) != 0 ) {
  196.             fputs("Error reading file",
  197.              stderr);
  198.         } else {
  199.             source[newLen++] = '\0';
  200.           /* Just to be safe add EOF */
  201.         }
  202.     }
  203. return source;
  204. }
  205.  
  206. void seekWord(char *w,char *sword,FILE *storage){
  207. char *token;
  208.  
  209.   token=strtok(w, " ");
  210.   while(token != NULL){
  211.         if(strcmp(sword,token)==0){
  212.           /* write to file*/
  213.          fprintf(storage, "word found: %s:: "
  214.          "search word is: %s\n",token,sword);
  215.        }
  216.   token=strtok(NULL, " ");
  217.   }
  218. }
  219. void printFile(char *f,int n)
  220. {
  221.   if(n==1)
  222.     printf("Source file\n");
  223.   else
  224.     printf("Results file:\n");
  225.  
  226.   size_t sz=strlen(f);
  227.   for(int i=0;i<sz;i++){
  228.      printf("%c",f[i]);
  229.   }
  230.  printf("\n");
  231. }
  232.  
  233. void chopToChars(char *source,char *searchwd,
  234. FILE *resultptr){
  235. char *t1,*ret;
  236. int hits=0,nohits=0,total=0;
  237.  
  238.     while((t1=strtok_r(source," ",&source))){
  239.  
  240.         ret=strstr(t1,searchwd);
  241.         if(!ret){
  242.     //if( (ret=strstr(t1,searchwd) == '\0' ) ){
  243.             nohits++;
  244.     }else{
  245.     hits++;
  246.           fprintf(resultptr, "\nhit num:[ %d ]:"
  247.           "word found::[[ %s ]]:: "
  248.           "search word is: %s\n\n\n",hits,t1,searchwd);
  249.     }
  250.       total++;
  251.      }
  252.     fprintf(resultptr, "total checks is [[ %d ]]\n"
  253.             "total misses is [[ %d ]]\n"
  254.         "total hits is   [[ %d ]]\n",
  255.         total,nohits,hits);
  256.  
  257. }
  258.  
  259.  
  260.  
  261. void usage(char *argv[]){
  262.  
  263.  printf("\nUsage:\n%s "
  264.     " -filename <argument> | -f <argument> \n"
  265.     " -searchword <argument> | -s <argument> \n"
  266.         " -print | -p  [prints results]\n"
  267.         " -printsource | -x [prints source file]\n",argv[0]);
  268.     exit(0);
  269. }
  270. void feedback(int c,int flag1,int flag2,
  271.               char * file, char * searchword,
  272.               int optopt,char * optarg){
  273.  
  274. printf("c = %d, flag1= %d, flag2= %d\n"
  275. " filename= %s sword= %s\n"
  276. "optopt= %c ,optarg= %s\n",
  277. c,flag1,flag2,file,searchword,optopt,optarg);
  278. }
  279.  
  280.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement