I'm solving a coding problem for a Data Structures assignment, and one part of my solution keeps giving me a segfault: 11
when reading the last line of a text file using fgets().
I've already searched around StackOverflow for some time now and can't find the same error I'm getting. I've tried many ways to solve the problem but I can't figure out what exactly is wrong.
int main() {
**S = func();
free(S);
return 0;
}
char** func() {
FILE *fStrings = fopen("file.txt", "r");
if (fStrings == NULL) printf("Error opening 'file.txt'\n")
int length = fileLength("file.txt"); // This returns just the number of lines in a file, works as intended.
char **strings = (char **)malloc(200 * length * sizeof(char));
f(length, (char (*)[200])strings, *fStrings);
// some code using the data modified by f()
fclose(fStrings);
return strings;
}
void f(int length, char strings[][200], FILE *fStrings) {
int i;
for (i = 0; i <= length; i++) {
printf("i = %d\n", i); // debug
fgets(strings[i], 200, fStrings); // Here's the problem
printf("string = %s\n", strings[i]); // debug
}
}
The above code has 2 debug lines to see where exactly the error happens. The function is called with the correct parameters, where length
is the amount of strings in the array, strings
is an array of strings, and fStrings
a file with said strings.
The segfault
occurs when trying to read the last line of the text file. Any help will be appreciated.
EDIT: Changed the code block to include a better version of my code, in case it makes it easier to understand. The correct libraries are included too.
strings[i]
as you do in theprintf
anyway? Where did you learn to do this, out of curiosity? – Sami Kuhmonenfor (i = 0; i <= length; i++) {
==>for (i = 0; i < length; i++) {
– 4386427