I'm trying to read a file and save the strings in an array using pointers, but I'm having problems. Can someone give me suggestions of what to do?
// not allowed to change these two rows
char **Lines;
Lines = (char**)malloc(sizeof(char*)*maxLines);
...
FILE *fp;
fp = fopen(fileName, "r"); // fileName already exists here
int i=0, j=0;
while(i<maxLines){
Lines[i] = (char*)malloc(maxLength * sizeof(char));
i++;
}
// No string will be longer than "maxLenght" so no buffer is used.
while(fgets(Lines[j] , maxLength, (FILE*) fp) != NULL && j < maxLines)
{
j++
}
I want to fill "Lines" with each string in the file. I keep getting segmentation fault. Thanks!