0

I want to know how many times a number appears in a string read from a file, but I'm not able to solve my problem.

Here is my C code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(){
   int MAX_MEM = 100000, i, count=0;
   char string[MAX_MEM];
   char search[MAX_MEM];
   FILE *fp;
   fp = fopen("prova1000.txt", "r");

   if (fp != NULL) {

       while (!feof(fp)){
           fread(string, sizeof(string), MAX_MEM, fp);
           printf("%s",string);
           char search[MAX_MEM];
           printf("\nEnter the number to search:");
           gets(search);
           char *equal = strstr(string,search);
           if(equal!= NULL){
               printf("The number introduced is in the list\n");
               while(equal!=NULL){
                  count++;
                  printf("The number is %d times\n", count);
               }
          }
          else{
              printf("The number introduced is not in the list\n");
           }
         }
     }

     else 
          printf("Couldn't open the file");

     return 0;
     fclose(fp);
  }

The prova1000.txt is something like this:

100000000000000
100000000000001
100000000000001
100000000000003
100000000000004
100000000000005
100000000000006
100000000000007
100000000000008
...

For example, I would want to show in count that 100000000000001 appears twice. How can I do that?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Bernat
  • 19
  • 1
    Well for starters there is an infinite loop at `while(equal!=NULL){` since `equal` doesn't change in the loop. And this is a perfect example of why the "short branch" goes before the "long" - That "couldn't open file" message floating out at the end is just plain confusing! – John3136 Mar 09 '17 at 22:22
  • 1
    worth your time to read through [this](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong),, and use [`fgets`](https://linux.die.net/man/3/fgets) instead of `gets` – yano Mar 09 '17 at 22:25

1 Answers1

0

I would break it up so it's not all in the main method. This will count the number of occurrences of that string in this method.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int wc(char* file_path, char* word){
    FILE *fp;
    int count = 0;
    int ch, len;

    if(NULL==(fp=fopen(file_path, "r")))
        return -1;
    len = strlen(word);
    for(;;){
        int i;
        if(EOF==(ch=fgetc(fp))) break;
        if((char)ch != *word) continue;
        for(i=1;i<len;++i){
            if(EOF==(ch = fgetc(fp))) goto end;
            if((char)ch != word[i]){
                fseek(fp, 1-i, SEEK_CUR);
                goto next;
            }
        }
        ++count;
        next: ;
    }
end:
    fclose(fp);
    return count;
}

int main(){//testestest : count 2
    char key[] = "test"; // the string I am searching for
    int wordcount = 0;

    wordcount = wc("input.txt", key);
    printf("%d",wordcount);
    return 0;
}
lakeIn231
  • 1,177
  • 3
  • 14
  • 34