2

Here is what I have right now:

int findKey(char *in, char *key, int buf){
    int count = 0;
    FILE *f;
    f = fopen(in,"r");
    char temp[buf];
    while(fgets(temp,buf,f) != NULL){
        if((strstr(temp,key))!= NULL){
            count++;
        }
    }
    fclose(f);
    return count;
}

I read through the txt file in, and look through it for key. If I find it then I increment count. However this seems only to work if there is only one occurance of key per like in the txt file. For example if the txt file is:

key Key key key

and char *key is "key" then count is only 1 but count should actually be 3. However if the txt file is:

key
key
Key
key

Then it returns the correct count (3). Not sure what's wrong here.

user2923535
  • 584
  • 1
  • 8
  • 22
  • 2
    1. `fgets` reads a whole line at a time (or up to the given maximum length) 2. `strstr` returns the **first** occurrence of the substring. – Kninnug Nov 27 '13 at 21:15
  • Your code can only detect a single occurrence per `buf` bytes, which in this case happens to be a single line. – Overv Nov 27 '13 at 21:16
  • fgets read a line at a time. If you want your keys are space delimited use fscanf(temp, "%s", word) – Jordonias Nov 27 '13 at 21:23

4 Answers4

2
int findKey(char *in, char *key, int buf){
int count = 0;
FILE *f;
f = fopen(in,"r");
char temp[buf];
while(fgets(temp,buf,f) != NULL){
    char *p = temp;
    while((p=(strstr(p,key)))!= NULL){
        count++;
        ++p;
    }
}
fclose(f);
return count;
}
notbad
  • 2,797
  • 3
  • 23
  • 34
  • Shouldn't that inner `if` be a `while`? – fpw Nov 27 '13 at 21:25
  • Awesome that worked! One more question, how would I make it so that it only identifies a full string like key but not ikey. `ikey key Key` should return only 1 but it right now returns 2. – user2923535 Nov 27 '13 at 21:40
  • @Not bad - though I'd recommend `findKey(const char *in, const char *key, int buf)`. – chux - Reinstate Monica Nov 27 '13 at 21:40
  • @user2923535 maybe you can use " key"(\backspace+key) instead of "key" – notbad Nov 27 '13 at 21:51
  • I don't think that will work because what if it's keyi instead. And in that case if I made it " key " then what if the txt file is `ikey key keyi key`. That should return 2 but I don't think it will if I changed the key to be " key " like that. – user2923535 Nov 27 '13 at 21:59
  • @user2923535 yeah. you can try to change key to be " key ", at the same time change in to be " in ". The way seems to be strange :) – notbad Nov 27 '13 at 22:17
1

fgets reads one line and strstr only tells you whether (and where) a string appears in that line.

Use another loop that calls strstr until it no longer finds the substring, i.e. use the result of strstr to know where to search next (increased by 1).

fpw
  • 801
  • 4
  • 12
  • Should this loop be inside the if statement? – user2923535 Nov 27 '13 at 21:21
  • It should replace the if statement because you need the return value of strstr. While it is not NULL, you must call strstr at the return value + 1 again. – fpw Nov 27 '13 at 21:23
1

the words have a terminator? like a space? if yes you could use this for determinate when a word finish. in this way you can build an array of words... and after works on this array

Giovanni Far
  • 1,623
  • 6
  • 23
  • 37
-1

Read the entire file or line by line into a std::string, then do this: Count character occurrences in a string

Community
  • 1
  • 1
Chromozon
  • 295
  • 2
  • 3
  • 8