I have this segment of two functions that should count the number of words of a file.txt. My problem is that fopen
don't stop of returning me the NULL value, so I can't run the function properly. I look more than one time and I'm sure that I have my file.txt in the folder.
I expect to return the number of words from texto.txt
int contadorPalabras(char str[]) {
int k, j;
k = 0;
j = 0;
while (str[k] != '\0') {
if (str[k] == ' ') {
j += 1;
while (str[k + 1] == ' ') {
k += 1;
}
}
k += 1;
}
if (str[0] == ' ') {
j -= 1;
}
if (str[k - 1] == ' ') {
j -= 1;
}
return (j + 1);
}
int palabras_en_txt() {
char cad[10000];
int total, j;
j = 0;
FILE *fichero;
fichero = fopen("texto.txt", "r");
if (fichero == NULL) {
printf("Hubo un problema en la apertura del archivo especificado.\n");
return 404;
}
while (feof(fichero) == 0) {
cad[j] = fgetc(fichero);
j += 1;
}
fclose(fichero);
printf("%s", cad[j]);
total = contadorPalabras(cad);
return total;
}