Thread: Help scanning words into array

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    2

    Help scanning words into array

    I'm trying to scan a list of words into an array based on some input from a text file. The first number indicates the amount of words in the text file. The longest word will be 29 characters (I'm still not sure how to do this). The dictWord is to store the words, but I don't know how to set the number in the array to the first number in the text file, and set the maximum length of the word to 29 characters.

    When I run this and attempt to print out the fourth word in the array to test if it's working, the program prints out this:

    רת"

    instead of words. Can anyone give advice as to why?

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        FILE* fin;
        fin=fopen("textmsg.txt", "r");    
        
        int i;
        int numWords;
            
        
        fscanf(fin, "%d", &numWords);
        
    
        char dictWord[numWords][29];
           
    
        for(i = 0; i < numWords; i++)
        {
            fscanf(fin, "%s", dictWord[i]);
        }
         
     
       printf("%s", dictWord[4]);
    
        
        system("PAUSE");
        return 0;
    }

    The format of the textmsg.txt file is as follows:

    4
    these
    are
    four
    words

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    Well, dictWord[4] is beyond the end of the array. The fourth word in the array is dictWord[3]
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    2
    Quote Originally Posted by NeonBlack View Post
    Well, dictWord[4] is beyond the end of the array. The fourth word in the array is dictWord[3]

    You're awesome. I forgot it's 0-3 rather than 1-4. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: excess elements in array initializer
    By redruby147 in forum C Programming
    Replies: 6
    Last Post: 09-30-2009, 06:08 AM
  2. Replies: 1
    Last Post: 04-25-2006, 12:14 AM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM
  4. extracting words from an array of words
    By axon in forum C++ Programming
    Replies: 2
    Last Post: 04-02-2003, 11:21 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM