简体   繁体   English

使用fgets()读取最后一行文件时的段错误

[英]segfault when reading last line of file using fgets()

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(). 我正在解决数据结构分配的编码问题,我的解决方案的一部分一直给我一个段错误segfault: 11当使用fgets()读取文本文件的最后一行时。

I've already searched around StackOverflow for some time now and can't find the same error I'm getting. 我已经在StackOverflow上搜索了一段时间,但是找不到同样的错误。 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. 上面的代码有2个调试行,以查看错误发生的确切位置。 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. 使用正确的参数调用该函数,其中length是数组中字符串的数量, strings是字符串数组, fStrings是带有所述字符串的文件。

The segfault occurs when trying to read the last line of the text file. segfault尝试读取文本文件的最后一行时,会发生。 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. 也包括正确的库。

I think the main problem with your code is this line 我认为你的代码的主要问题是这一行

for (i = 0; i <= length; i++) {

Due to <= it will loop "length + 1" times but you have only allocated memory for "length" times. 由于<=它将循环“长度+ 1”次,但您只为“长度”时间分配了内存。 Change it to: 将其更改为:

for (i = 0; i < length; i++) {

Your allocation is strange. 你的分配很奇怪。 You should allocate a 2D array using a pointer to a 1D array, ie like this: 您应该使用指向1D数组的指针来分配2D数组,例如:

char (*strings)[200] = malloc(length * sizeof *strings)

then you can do the call without any cast. 然后你可以在没有演员的情况下进行通话。

Also just noticed this line: 还注意到这一行:

f(length, (char (*)[200])strings, *fStrings);
                                  ^
                                  notice

I don't think you want * in front of fStrings (it should not even compile with the * ) 我不认为你想在fStrings前面* (甚至不应该用*编译)

With the correct allocation (as described above) the call should be: 通过正确的分配(如上所述),呼叫应该是:

f(length, strings, fStrings);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM