0

This is the code that was provided as starter code on which I have to answer a couple of question using the data provided in data.txt file. data.txt is kept in the same folder as my code and contains line separated words all of which are of length 21. I am familiar with C but I do not know anything related to files and file management.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

int main(void) {
    char* fname = "data.txt";
    FILE *fptr = NULL;
    char line[20000][22];
    int i = 0;
    fptr = fopen(fname, "r");
    while(fgets(line[i],20000,fptr))
    {
        line[i][strlen(line[i]) - 1] = '\0';
        i++;
    }
    printf("Read a file with %d lines.\n",i);
}

When I run this code in CLion, the following error appears.

error image

I am using LLVM clang compiler. The same error appears even if I use Microsoft Visual Studio compiler. It would be helpful if you can explain what the error means and how to resolve it!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Looks like you want `char line[22][20000];` rather than `char line[20000][22];`. Also you should check that your file does not contain more lines. – Eugene Sh. Nov 12 '20 at 20:11
  • If you just want to count the number of lines, not actually use the contents of the file, then read character by character, and keep a "newline" counter which is increased when you read a newline. If you still want to use `fgets` then you don't need an array of arrays, just a single array for the line. You also should check that `fopen` doesn't fail, you don't need to remove the ending newline, and please name your counter variable something more descriptive (like `line_count` or similar). – Some programmer dude Nov 12 '20 at 20:16
  • @EugeneSh. No, `line[22][20000]` would only accommodate 22 words each having maximum length of 20000 characters. I have checked, the file indeed contains exactly 20000 lines each line containing a single word of length 21. Also, this code runs on repl.it but doesn't run on my machine – Harshil Purohit Nov 12 '20 at 20:16
  • @Someprogrammerdude The print statement is just for sanity check – Harshil Purohit Nov 12 '20 at 20:17
  • With `fgets(line[i],20000,fptr)` you read *up to* `20000` characters into an array of only 22 characters (null-terminator included). – Some programmer dude Nov 12 '20 at 20:18
  • @Harshil Then you are not using it correctly. `fgets(line[i],20000,fptr)` will read up to 20000 characters into a *single* `line[i]` - which it is obviously doesn't have space for (it has space for 22). – Eugene Sh. Nov 12 '20 at 20:18
  • 1
    By the way, have you *read* the error message? The crash seems to happen inside `fgets`, and the problem is seems to be that `stream.valid()` fails. Are you sure that you opened the file? What does `fopen` return? Maybe its time to add some error checking in your code? – Some programmer dude Nov 12 '20 at 20:21
  • @Someprogrammerdude I replaced the 20000 in fgets with 22. Same error is popping up – Harshil Purohit Nov 12 '20 at 20:27
  • 2
    "each line containing a single word of length 21". You have forgotten the newline. Array size needed is `21 + 1 + 1 = 23`. – Weather Vane Nov 12 '20 at 20:28
  • 1
    ... use `while(fgets(line[i], sizeof line[i], fptr))` **and** check `i` stays within bounds. Input has a nasty habit of not conforming to your expectations. – Weather Vane Nov 12 '20 at 20:37
  • Have you changed the array dimension to `23` as advised? Be bold: try `char line[21000][30];`. Also, you are assuming that the last character is a newline: [this method](https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input/28462221#28462221) is bullet proof. – Weather Vane Nov 12 '20 at 20:50
  • @WeatherVane Your calculation of array size 23 seems to be correct. `fgets(line[i], 23, fptr)` on replit doesnt throw an error. However, CLion still doesnt work – Harshil Purohit Nov 12 '20 at 20:55

0 Answers0