J
Well, you just call once. fgets(conteudo, 98, arquivo). What if the file has more than 98 characters? So you should do a loop and go reading until you have nothing more to read. Something like this:#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// conta quantas vezes a palavra aparece no arquivo
int contaOcorrencias(char *nomeArquivo, char *palavra){
char conteudo[100];
FILE *arquivo = fopen(nomeArquivo, "r");
if (arquivo == NULL) {
printf("Erro ao abrir arquivo %s\n", nomeArquivo);
exit(-1); // se deu erro, sai
}
int cont = 0;
// loop para ir lendo o arquivo
while (fgets(conteudo, 100, arquivo) != NULL) {
char *tmp = conteudo;
while ((tmp = strstr(tmp, palavra)) != NULL) {
cont++; // encontrei uma ocorrência
tmp++; // continua a busca a partir da posição seguinte
}
}
fclose(arquivo);
return cont;
}
int main() {
// declara o nomeArquivo, palavra, etc...
// obtém a quantidade de vezes que a palavra ocorre no arquivo
int ocorrencias = contaOcorrencias(nomeArquivo, palavra);
// faz o que quiser com o valor (printf, etc)
printf("A palavra \"%s\" ocorre %d vezes no arquivo %s\n", palavra, ocorrencias, nomeArquivo);
return 0;
}
It is worth remembering fgets reads up to the informed character limit, or until you find a line break (which occurs first). There is usually a concern to remove this line break, but in this case I do not think necessary, since you will search for the word, and the line break at the end will not influence the search (at least of course, that the "word" has line breaks). I am also assuming that there are no words "breaking" (that there was separation of syllables and it continues on the line under, for example).Then I use https://en.cppreference.com/w/c/string/byte/strstr , which verifies whether a string occurs within another, and returns a pointer to the character where the occurrence begins (or NULL if you do not find anything). I make a loop for it may be that the word occurs more than once on the same line, and as I find, I will update the counter.It is worth remembering that this approach is naive, because if I look for "cutting" and in the file you have the words "sounding" or "corrosive", both will be accounted for. If you want to be more accurate (consider only the word "cut", ignoring the cases in which it is part of a word), then it already complicates a little more, because you would have to analyze the phrase, checking if there are separators (spaces, punctuation signs, etc).One option is to use https://www.cplusplus.com/reference/cstring/strtok/ to separate the string into parts:// conta quantas vezes a palavra aparece no arquivo
int contaOcorrencias(char *nomeArquivo, char *palavra) {
char conteudo[100];
FILE *arquivo = fopen(nomeArquivo, "r");
if (arquivo == NULL) {
printf("Erro ao abrir arquivo %s\n", nomeArquivo);
exit(-1); // se deu erro, sai
}
int cont = 0;
char *delimiters = " ,.-;!?";
while (fgets(conteudo, 100, arquivo) != NULL) {
char *tok = strtok(conteudo, delimiters);
while (tok != NULL) {
if (strcmp(tok, palavra) == 0)
cont++;
tok = strtok(NULL, delimiters);
}
}
fclose(arquivo);
return cont;
}
How to tab I used " ,.-;!?", that is, it separates the parts using space, comma, point or any of the other characters (adapts to your case), which resolves the case already mentioned in which the word to be sought is part of another larger word.