I'm trying to read text from a .txt file and count the amount of words in it using c

88 Views Asked by At

This is what I have so far. The issue is that the "count" variable is not properly adding to itself when it hits a newline or space character.

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

int main(void){

    char c;
    int count = 0;
    int* ptr;
    int size;

    FILE *file = fopen("file.txt", "r"); //Reads from input file

    //Test to see if the file exists
    if (file == NULL){
        printf("ERROR 1: TARGET INPUT FILE DOES NOT EXIST, HAS BEEN RENAMED OR HAS BEEN MOVED.");
        exit(1);
    }

    c = fgetc(file);
    while ((c = fgetc(file)) != EOF){

        if(c == " " || c == "\n"){
            count++;
        }

    }

    fclose(file);

    printf("Number of words present in given file: %d", count);  
    return 0;
}

I don't have much of an idea as to why it is failing, although I have looked up multiple sources that have pretty much this exact code that seems to work for them. The output should return a number representing the amount of words in the .txt file.

3

There are 3 best solutions below

1
Vlad from Moscow On BEST ANSWER

For starters the variable c should be declared as having type int instead of char.

Also ths call of fgetc before the while loop

c = fgetc(file);

is ignored

You are comparing an integer with pointers

if(c == " " || c == "\n"){

because string literals in the above expressons are converted to pointers to their first characters. The compiler should issue a message for this if statement.

But if you will write instead

if(c == ' ' || c == '\n'){

using character constants nevertheless the approach is incorrect because it does not take into account adjacent spaces.

3
San Zhang On

try this

while ((c = fgetc(file)) != EOF){
    if((c>='A'&& c<='Z') || (c>='a'&& c<='z')){
        while ((c = fgetc(file)) != EOF){
            if(c == '\n' || c == ' '){
                count++;
                break;
            }
        }
    }

}

in initial code, if I have "fsdfs \n asfsf " in file, count may be larger than it should be.

0
Jon Green On
  1. fgetc() returns an int, not a char. So c should be declared int.
  2. You read the first character of the file...and discard it. Did you mean to do this? I can see a good reason for doing it this way, I'm just not sure you were thinking the same way.
  3. In if(c == " " || c == "\n"), you're comparing your single character against strings, because you've double-quoted " " and "\n". You can't do this in C - or, at least, the results will be rather random. Use single quotes instead, so if(c == ' ' || c == '\n'). That way, you're comparing against single character values.
  4. You declared int* ptr; and int size; but didn't use them. Might as well delete those lines. A decent compiler will warn you about unused variables.
  5. Your approach is incorrect, in any case. If your file contained a lot of empty lines, or more than one space between words, your word count would be incorrect. You should be counting the number of times non-whitespace is immediately followed by whitespace (or EOF), and ignoring repeated whitespace. I'll leave that as an exercise for the student!