c - 逐行存储文件的功能会产生奇怪的结果

标签 c io

我编写了一个函数,它接受一个文件并将每一行存储在一个字符串数组中。

这里是涉及到的函数:

char **get_lines(FILE *file) {
    int MAX_LINE_LENGTH = 2048;
    int num_lines = 19;
    char **lines = malloc(sizeof(char *) * num_lines);

    // Make sure to start at the beginning of the file
    fseek(file, 0L, SEEK_SET);

    char *line = malloc(MAX_LINE_LENGTH);

    int i = 0; // index counter for lines

    while (fgets(line, MAX_LINE_LENGTH, file)) {
        // Only add to lines if the line is not a single newline character
        if (strcmp(line, "\n")) {

            int len = strlen(line);

            // Remove the last character as
            // it should always be a newline character
            if (len < MAX_LINE_LENGTH) {
                line[len-1] = '\0';
            }

            lines[i] = malloc(strlen(line));
            lines[i++] = line;
            printf("%s\n", lines[i-1]); // This prints the lines correctly.
        }
    }

    for (int x=0; x < num_lines; x++) {
        // This prints strings containing only a newline character.
        printf("%s", lines[x]); 
    }

    return lines;
}

令我困惑的是,如果我在将它添加到数组后立即打印该行(在 while 循环内),则会打印正确的字符串。但是,如果我在 lines 定义完成后打印每一行(在 while 循环之外),它只会打印包含换行符的字符串。

这可能是什么原因造成的?

编辑 在不修改程序之后,现在第二个 printf 语句不会向控制台打印任何内容。

如果有帮助,我正在使用 CLion。

最佳答案

代码正在泄漏内存,因为它忽略了 malloc 内部循环,并且正在将 line 的相同地址分配给 lines[i]

char **get_lines(FILE *file) {
int MAX_LINE_LENGTH = 2048;
int num_lines = 19;
char **lines = malloc(sizeof(char *) * num_lines);

// Make sure to start at the beginning of the file
fseek(file, 0L, SEEK_SET);

char *line = malloc(MAX_LINE_LENGTH);

int i = 0; // index counter for lines

while (fgets(line, MAX_LINE_LENGTH, file)) {
    // Only add to lines if the line is not a single newline character
    if (strcmp(line, "\n")) {

        int len = strlen(line);

        // Remove the last character as
        // it should always be a newline character
        if (len < MAX_LINE_LENGTH) {
            line[len-1] = '\0';
        }

        lines[i] = malloc(strlen(line));
        strncpy(lines[i], line, len);
        i++;
        printf("%s\n", lines[i-1]); // This prints the lines correctly.
    }
 }

for (int x=0; x < num_lines; x++) {
    // This prints strings containing only a newline character.
    printf("%s", lines[x]); 
}

 return lines;
}

关于c - 逐行存储文件的功能会产生奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50418040/

相关文章:

c - 具有 FIFO 的结构

c - 新旧GCC生成的汇编代码for循环的区别

c - 如何正确获取一行并用 C 解析它

c - 如何清除 C 中的输入缓冲区?

c - a+++++b 和 a+++++b 的区别

c - 如何定义编译时 -D 宏 (Apache 2.2)?

io - 将未格式化(二进制数据)写入标准输出

java - 如何在不使用非 api 类的情况下取消 Java 中的 Files.copy() ?

c# - Metro 类库,FileInfo 替代品?

c++ - 为什么下面的数组解引用指针具有相同的值 : the pointer to an array, ?