Thread: Read Strings From Text File Into Char Array

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    27

    Read Strings From Text File Into Char Array

    Good afternoon. I am writing a program that needs to open a text file, read the number of words in said file. and then, using a loop, load those words into elements of an array. The dictionary file won't have anymore than 10,000 words.

    My program compiles without issue, but when it runs, it prints the number of entries in my test dictionary to test that the number is being ready properly by my program, and then has a windows error at the next fscanf when I am attempting to read each word into an array element.

    Thanks for your time.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
    
    int i, wordCount;
    char wordBank[9999];
    
    FILE * ifp = fopen("dictionary.txt", "r"); //open dict file
    
    fscanf (ifp, "%d ", &wordCount); //read number of words in dictionary
    printf ("%d\n", wordCount); //verified number is being read
    
    for (i = 0; i < wordCount; i++) //load wordbank array with words
    
        {
    
        fscanf (ifp, "%s", wordBank[i]);
        printf ("%s \n", wordBank[i]);
    
        }
    
    fclose; //close dict file
    
    }

  2. #2
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    The file format of dictionary.txt is as follows:

    Code:
    5
    apple
    bear
    cat
    devil
    poop

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    char wordBank[9999];
    This declares an array of 9999 characters. If you want an array of [character arrays] strings, you need to use a two-dimensional array. The second dimension should be large enough to hold the largest word (plus the null character).

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    That's it! I have not programmed in a year and can't believe I forgot about this. Thank you very much.

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Also, if it has 10000 words, why are you only defining the array as 9999? Don't you want the last word?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    I always get confused about the zero element of arrays vs making them the right size.

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Simplest way to remember:
    1) Put the number of elements in the definition array[TOTAL] . Don't think about it, just the TOTAL.
    2) Your loop always starts at 0. Just get used to it.
    3) The loop goes to < TOTAL. Never use =.
    Last edited by WaltP; 09-12-2012 at 11:25 PM.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    27
    Thanks for the pointers.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    The way I remember is: The declaration is the number of elements defined and the implementation is an offset from the first element.

    i.e.
    Code:
    int arr[n] // has n elements
    
    var = arr[i] // is the 'i'th integer (in this case) after the start of the array
    Last edited by Click_here; 09-13-2012 at 12:42 AM. Reason: bad wording

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Strings in from text file and store into array
    By RRTT in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2011, 04:52 AM
  2. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 03:26 PM
  3. Read (BIG) File into char array
    By doia in forum C Programming
    Replies: 2
    Last Post: 03-19-2010, 05:26 PM
  4. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  5. Read File To Char Array with Null char init
    By MicroFiend in forum Windows Programming
    Replies: 1
    Last Post: 10-28-2003, 06:18 PM