need help FAST, counting words in a file with fscanf

This is a discussion on need help FAST, counting words in a file with fscanf within the C Programming forums, part of the General Programming Boards category; i have a int number which is supposed to count the number of words in a file, but its not ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    need help FAST, counting words in a file with fscanf

    i have a int number which is supposed to count the number of words in a file, but its not augmenting correctly, stays at 0, dont know why i need help

    Code:
    int number = 0;
    
    fp = fopen("wordlist", "r");
    		  
        while (fscanf(fp, "%s", word) == 1) {
            number++;
        }
    
        fclose(fp);
    when i try to printf number it gives 0 even when the file definitely has more than that. the file im trying to give to it has one word on each line with no numbers or non-letters
    im guessing the line with fscanf is wrong how can i fix it?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    There seems to be nothing wrong with that code to me. Well, nothing wrong is relatively, because of course there's a lacking check if fopen succeeded.

    But it works as expected for me. So the problem must be somewhere else... For instance, what is the variable "word"?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    As hinted at, if its staying at zero it means only one thing: the body of the while loop is not being executed. So check the return value of the fopen call to make sure it succeeded. You could also move the fscanf to before the while loop, and save and print its return value. If both of these dont bring up any issues then we need more context (code, example of file, etc).

    Also, I would try to use fgets instead of fscanf.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    char is declared at top as

    char* word = NULL;


    also, i have tried an if statement to check that the file isnt null and indeed its not.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    That would be your problem. Try: char word[100];
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    So your telling the function to read from the file and store the result into the "word" variable, which hasnt been given any memory--it is NULL. So allocate some memory, either statically (by giving an explicit array size, i.e. "char word[10]") or dynamically (by using malloc, etc).

    You should tell fscanf the maximum size of your string buffer, or just use fgets, otherwise there is a chance of buffer overflow.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    the thing is, i dont know the size of the file, how many words will be in it, could be 3, could be 10000

    i just hardcoded to simplify, but its supposed to be decided by user

    so should i declare it using malloc or something? what's the best way to do this

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Well you could just define some "max" string length that you (reasonably) anticipate. Some multiple of 2 is usually given (though completely arbitrary), for example 128, 256, 512. You would probably have words less than that, right?

    If not, the perfect solution would be to read characters of the file up to a newline character, counting how many characters up to the newline. You then know how long the word/line is (to count "words", count the number up to " " space character instead of newline). You can then "rewind" the file that number of characters, allocate that amount of memory using malloc, then read it again into the variable.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    The size of "word" is not the maximum number of words, it's the maximum size a word may have. In your case, if it is bigger, your application will probably crash. If words can be of any length, you need more advanced methods as stated before.

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    11
    ok would i then be able to pass this word into a function that has char* in one of its parameters?

    The file reading appears to be functional now but I'm getting a segmentation fault when I try to use word in a function

    like

    InsertList(char* item)
    would i be able to pass the word that was declared as char *word[45] to this?
    Last edited by busdude; 12-04-2009 at 05:19 PM.

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,159
    Quote Originally Posted by nadroj View Post
    Well you could just define some "max" string length that you (reasonably) anticipate. Some multiple of 2 is usually given (though completely arbitrary), for example 128, 256, 512. You would probably have words less than that, right?

    If not, the perfect solution would be to read characters of the file up to a newline character, counting how many characters up to the newline. You then know how long the word/line is (to count "words", count the number up to " " space character instead of newline). You can then "rewind" the file that number of characters, allocate that amount of memory using malloc, then read it again into the variable.
    Using rewind means that you can't operate on non-seekable files (e.g. stdin, sockets, pipes, etc)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Don't declare it as char *word[45]. It's just char word[45].
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by brewbuck View Post
    Using rewind means that you can't operate on non-seekable files (e.g. stdin, sockets, pipes, etc)
    Sure, but the question was for "a file" that he/she has. I think this is reasonably assumed to be a "normal" random-access file on permanent storage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Counting Number of Words in a Text File Using C
    By wvu2005 in forum C Programming
    Replies: 16
    Last Post: 09-27-2005, 11:45 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. counting words in a text file...
    By flightsimdude in forum C Programming
    Replies: 10
    Last Post: 09-19-2003, 07:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21