I'm currently working on a code that must be able to read in a line from a preexisting file line by line, separating different strings in the line with strep(). Currently, it is successfully reading and printing each line (of which there are 47 total). However, after it reads and prints the last line, it encounters a segmentation fault. To my understanding, this means that the code tries to access something that doesn't exist (ex. array element 8 in "int array[5];". I don't understand at what point it tries to access such an element.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
FILE *fp = fopen(argv[1], "r");
int i = 0, j;
char line[47][500];
fgets(line[i], 500, fp);
printf("%d", i);
printf("%s\n", line[i]);
i++;
while (!feof(fp)) {
while (fgets(line[i], 500, fp) != NULL && i < 47) {
printf("%d", i);
printf("%s\n", line[i]);
i++;
}
}
There is some code after this, but this is where is encounters the error.