Like Tree2Likes
  • 1 Post By AndiPersti
  • 1 Post By Salem

How to read a text file into an array of strings?

This is a discussion on How to read a text file into an array of strings? within the C Programming forums, part of the General Programming Boards category; In a program, I have a text file (called MyDictionary.txt) which has thousands of words in alphabetical order. I need ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    8

    How to read a text file into an array of strings?

    In a program, I have a text file (called MyDictionary.txt) which has thousands of words in alphabetical order. I need to make a C program that reads in this text file and then makes an array called

    char Words[# of total words in the text file][length of longest word].

    How do i do that??

    if it helps, this is kind of what the dictionary looks like, except many more words:

    Aarhus
    Aaron
    Ababa
    aback
    abaft
    abandon
    if you could give me exactly how to do this that would be great, because I have very little time. Thanks for all the help.

  2. #2
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Do you know beforehand how many words there are, and how long the longest word is? If so,

    Code:
    char Words[NUMBER_OF_WORDS][LENGTH_OF_LONGEST_WORD+1]; /*the +1 leaves room for the \0 that terminates a string*/
    To read from a file, use the fgets function.
    Code:
    while(!asleep) {
       sheep++;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    8
    Quote Originally Posted by TheBigH View Post
    Do you know beforehand how many words there are, and how long the longest word is? If so,

    Code:
    char Words[NUMBER_OF_WORDS][LENGTH_OF_LONGEST_WORD+1]; /*the +1 leaves room for the \0 that terminates a string*/
    To read from a file, use the fgets function.
    Thanks for the reply, but no we do not get the number of words beforehand, we have to find that out ourselves, which is what I don't understand how to do.

  4. #4
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    The number of words does not need to be known. The length of the longest word does, though. Create a list of pointers to characters:

    Code:
    char * word_list[ MAXWORDLENGTH ];
    Then read in each word from the file, allocate enough space using malloc for the word in the current spot in the list, copy the word into the space, and repeat until no more words.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by jwroblewski44 View Post
    The number of words does not need to be known. The length of the longest word does, though. Create a list of pointers to characters:
    Code:
    char * word_list[ MAXWORDLENGTH ];
    Then read in each word from the file, allocate enough space using malloc for the word in the current spot in the list, copy the word into the space, and repeat until no more words.
    Well, actually it's the other way round. In your example "word_list" is an array of MAXWORDLENGTH pointers to char, i.e. you can store MAXWORDLENGTH words with any number of characters if you allocate enough memory for each word.

    To the OP: Do you know how to dynamically allocate memory using malloc()?

    If not you could read the file once, counting the lines (assuming there is only one word per line) to find out how many words are in the file. Then create the array and read the file a second time, now storing each word in the array. Not very efficient so I think you are supposed to use dynamic memory allocation, aren't you?

    Bye, Andreas
    jwroblewski44 likes this.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    AndiPersti likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Quote Originally Posted by AndiPersti View Post
    Well, actually it's the other way round. In your example "word_list" is an array of MAXWORDLENGTH pointers to char, i.e. you can store MAXWORDLENGTH words with any number of characters if you allocate enough memory for each word.

    To the OP: Do you know how to dynamically allocate memory using malloc()?

    If not you could read the file once, counting the lines (assuming there is only one word per line) to find out how many words are in the file. Then create the array and read the file a second time, now storing each word in the array. Not very efficient so I think you are supposed to use dynamic memory allocation, aren't you?

    Bye, Andreas
    Ahh yes, thank you for pointing that out. I had the number of characters confused with the number of words.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Strings From Text File Into Char Array
    By somniferium in forum C Programming
    Replies: 8
    Last Post: 09-13-2012, 01:41 AM
  2. 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
  3. Read max value from strings of numbers in text file
    By james890 in forum C++ Programming
    Replies: 14
    Last Post: 04-15-2010, 04:26 PM
  4. Read Text File into an Array
    By rassul in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 12:26 AM
  5. Read text from a file into an array
    By ccwash in forum C++ Programming
    Replies: 1
    Last Post: 10-26-2005, 04:19 PM

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