Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985295
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:39:43+00:00 2026-05-27T18:39:43+00:00

Is there a way to read a text file into a one dimensional array

  • 0

Is there a way to read a text file into a one dimensional array in plain C? Here’s what I tried (I am writing hangman):

int main() {
    printf("Welcome to hangman!");

    char buffer[81];
    FILE *dictionary;
    int random_num;
    int i;
    char word_array[80368];

    srand ( time(NULL) );

    random_num = rand() % 80368 + 1;
    dictionary = fopen("dictionary.txt", "r");

    while (fgets(buffer, 80, dictionary) != NULL){
        printf(buffer); //just to make sure the code worked;
        for (i = 1; i < 80368; i++) {
            word_array[i] = *buffer;
        }
    }

    printf("%s, \n", word_array[random_num]);
    return 0;
}

What’s wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:39:44+00:00Added an answer on May 27, 2026 at 6:39 pm

    Try changing a couple of things;

    First; you’re storing a single char. word_array[i] = *buffer; means to copy a single character (the first one on the line/in the buffer) into each (and every) single-char slot in word_array.

    Secondly, your array will hold 80K characters, not 80K words. Assuming that that’s the length of your dictionary file, you can’t fit it all in there using that loop.

    I’m assuming you have 80,368 words in your dictionary file. That’s about 400,000 words less than /usr/share/dict/words on my workstation, though, but sounds like a reasonable size for hangman…

    If you want a one-dimensional array intentionally, for some reason, you’ll have to do one of three things:

    • pretend you’re on a mainframe, and use 80 chars for every word:

        char word_array[80368 * 80];
      
      memcpy (&(word_array[80 * i]), buffer, 80);
      
    • create a parallel array with indices to the start of each line in a huge buffer

         int last_char = 0;
         char* word_start[80368];
         char word_array[80368 * 80];
         for ( … i++ ) {
             memcpy (&word_array[last_char], buffer, strlen(buffer));
             word_start[i] = last_char;
             last_char += strlen(buffer);
         }
      
    • switch to using an array of pointers to char, one word per slot.

        char* word_array[80368];
      
        for (int i = 0; i < 80368, i++) {
             fgets (buffer, 80, dictionary);
             word_array[i] = strdup (buffer);
        }
      

    I’d recommend the latter, as otherwise you have to guess at the max size or waste a lot of RAM while reading. (If your average word length is around 4-5 chars, as in English, you’re on average wasting 75 bytes per word.)

    I’d also recommend dynamically allocating the word_array:

       int max_word = 80368;
       char** word_array = malloc (max_word * sizeof (char*));
    

    … which can lead you to a safer read, if your dictionary size ever were to change:

       int i = 0;
       while (1) {
            /* If we've exceeded the preset word list size, increase it. */
            if ( i > max_word ) {
                max_word *= 1.2; /* tunable arbitrary value */
                word_array = realloc (word_array, max_word * sizeof(char*));
            }
            /* Try to read a line, and… */
            char* e = fgets (buffer, 80, dictionary);
            if (NULL == e) { /* end of file */
                /* free any unused space */
                word_array = realloc (word_array, i * sizeof(char*));
                /* exit the otherwise-infinite loop */
                break;
            } else {
                /* remove any \r and/or \n end-of-line chars */
                for (char *s = &(buffer[0]); s < &(buffer[80]); ++s) {
                   if ('\r' == *s || '\n' == *s || '\0' == *s) {
                      *s = '\0'; break;
                   }
                }
                /* store a copy of the word, only, and increment the counter.
                 * Note that `strdup` will only copy up to the end-of-string \0,
                 * so you will only allocate enough memory for actual word
                 * lengths, terminal \0's, and the array of pointers itself. */
                *(word_array + i++) = strdup (buffer);
            }
        }
        /* when we reach here, word_array is guaranteed to be the right size */
        random = rand () % max_word;
        printf ("random word #%d: %s\n", random, *(word_array + random));
    

    Sorry, this is posted in an hurry, so I haven’t tested the above. Caveat emptor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to read value for the WiX variable from a text
Is there a way to read a locked file across a network given that
Is there a way to read a module's configuration ini file? For example I
Is there a way to read off from an [external] xml (an xml file
Is there any way to read a file using sharpsvn................
Is there any way to read a TIFF file using AIR ? Looks like
In C#/.NET (on Windows) is there a way to read a growing file using
Is there a way I can read the contents of plist file and put
Is there a way to write python data structs to a file as text.
I'm trying to read the contents of a text file into the attributes of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.