Help scanning words into array

This is a discussion on Help scanning words into array within the C Programming forums, part of the General Programming Boards category; I'm trying to scan a list of words into an array based on some input from a text file. The ...

  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
    435
    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, 07:08 AM
  2. Replies: 1
    Last Post: 04-25-2006, 01:14 AM
  3. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 01: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, 09:31 AM

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