0

I have read a lot of questions on this, and using them I have altered my code and have created code which I thought would work.

I think it's my understanding of C, which is failing me here as I can't see where I'm going wrong.

I get no compilation errors, but when I run i receive 'FileReader.exe has stopped working' from the command prompt.

My code is :

void storeFile(){
int i = 0;
char allWords [45440][25];
    FILE *fp = fopen("fileToOpen.txt", "r");
    while (i <= 45440){
        char buffer[25];
        fgets(buffer, 25, fp);
        printf("The word read into buffer is : %s",buffer);
        strcpy(allWords[i], buffer);
        printf("The word in allWords[%d] is : %s", i, allWords[i]);
        //allWords[i][strlen(allWords[i])-1] = '\0';
        i = i + 1;
    }
    fclose(fp);

}

There are 45440 lines in the file, and no words longer than 25 char's in length. I'm trying to read each word into a char array named buffer, then store that buffer in an array of char arrays named allWords.

I am trying to get this part working, before I refactor to return the array to the main method (which I feel won't be a fun experience).

8
  • 4
    while (i <= 45440){: you'll have a problem at the last iteration: buffer overflow Commented Feb 3, 2017 at 11:56
  • 2
    If a word length on a line is 25, the buffer length needed to read it in entirety is 27 (allowing for the '\n' and zero terminator). Otherwise, the line will be read in two parts. Your loop i <= 45440 will also write to allWords[45411] which is a buffer overrun Commented Feb 3, 2017 at 11:58
  • @Peter: it's been a while but I thought that fgets reads 25 - 1 characters. Commented Feb 3, 2017 at 11:59
  • 3
    @Bathsheba - some introductory texts have historically taught that. They are wrong. Commented Feb 3, 2017 at 12:00
  • See en.cppreference.com/w/c/io/fgets. Suggests that count - 1 is read. Commented Feb 3, 2017 at 12:01

1 Answer 1

5

You are trying to allocate more than a megabyte (45440*25) worth of data in automatic storage. On many architectures this results in stack overflow before your file-reading code even gets to run.

You can work around this problem by allocating allWords statically, like this

static char allWords [45440][25];

or dynamically, like this:

char (*allWords)[25] = malloc(45440 * sizeof(*allWords));

Note that using buffer in the call to fgets is not required, because allWords[i] can be used instead, without strcpy:

fgets(allWords[i], sizeof(*allWords)-1, fp);

Also note that an assumption about file size is unnecessary: you can continue calling fgets until it returns NULL; this indicates that the end of the file has been reached, so you can exit the loop using break.

Sign up to request clarification or add additional context in comments.

2 Comments

It still (desperately) needs those other fixes mentioned in the comments!
Ah that's sorted it. I have also increased the size to 27 as read in the comments to allow for the characters I didn't take into account. Thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.