使用 BUFFER_SIZE 大于 1 的 Get Next Line 进行线路管理时出现问题

问题描述 投票:0回答:1

我目前正在为我的学校开展 Get Next Line 项目,该项目涉及从文件描述符 (fd) 读取并逐行返回读取的行。我已经成功实现了 BUFFER_SIZE 为 1 的功能,但是当我尝试使用大于 1 的 BUFFER_SIZE 时我遇到了困难。

具体来说,当我一次读取两个字符并且我正在读取的行没有偶数个字符时,我不知道如何处理下一行的剩余字符。

你能帮我理解如何处理剩余的字符并实现一个解决方案来正确处理使用大于 1 的 BUFFER_SIZE 读取的行吗?

这是我的代码。

#include "get_next_line.h"

char    *get_next_line(int fd)
{
    char            *line;
    static char     *cache = NULL;

    if (fd < 0 || BUFFER_SIZE <= 0)
        return (NULL);

    line = ft_read_until_nl(cache, fd);
    return (line);
}

char    *ft_read_until_nl(char *cache, int fd)
{
    char    *str;
    int     read_bytes;
    char *string;
    int i = 0;

    str = malloc(sizeof(char) * BUFFER_SIZE + 1);
    read_bytes = 1;
    while (read_bytes && !ft_strchr(str, '\n'))
    {
        read_bytes = read(fd, str, BUFFER_SIZE);

        if (read_bytes == -1)
        {
            free(cache);
            return(NULL);
        }
        str[read_bytes] = '\0';
        cache = ft_strjoin(cache, str);
        if (ft_strchr(str, '\n'))
            break;
        i++;
    }
    string = malloc(sizeof(char) * ft_strlen(cache) + 1);
    ft_strcpy(string, cache);
    return (string);
}



int main() {
    printf("BUFFER_SIZE: %d", BUFFER_SIZE);
    int fd = open("file.txt", O_RDONLY);
    int lines = 4;
    int i = 0;

    while (i < lines) {
    printf("\nline %d -> %s", i, get_next_line(fd));
        i++;
    }

    return (0);
}
c file-io buffer getline static-variables
1个回答
0
投票

不要忘记保护你的 malloc。您需要第二个 Funktion 来创建您想要返回的行。第一个 Funktion 应该做的,只是缓冲区。

你的 get_next_line() 主要功能应该是这样的。 (但是你需要自己写的其他功能)exam 03 也可以是 get_next_line 所以试着去理解它。

你忘了放 (BUFFER_SIZE + 1) 你记得 * 会先计算。但是你想先完成 + 所以使用 BUFFER_SIZE * (.. + 1);

您还需要更改 ft_strjoin。因为当你得到一个带有 NULL 的字符串时,它不会 malloc 任何东西所以你需要稍微改变它。

char    *ft_read_until_nl(char *cache, int fd)
{
    char    *str;
    int     read_bytes;

    str = malloc(sizeof(char) * (BUFFER_SIZE + 1));
    if (!str)
        return (NULL);
    read_bytes = 1;
    while (!ft_strchr(str, '\n') && read_bytes != 0)
    {
        read_bytes = read(fd, str, BUFFER_SIZE);
        if (read_bytes == -1)
        {
            free(cache);
            return(NULL);
        }
        str[read_bytes] = '\0';
        cache = ft_strjoin(cache, str);
    }
    free(str);
    return (cache);
}

char    *get_next_line(int fd)
{
    static char *cache;
    char        *line;

    if (fd < 0 || BUFFER_SIZE <= 0)
        return (NULL);
    cache = ft_read_until_nl(cache, fd);
    if (!cache)
        return (NULL);
    line = ft_putline(cache);
    cache = ft_next_str(cache);
    return (line);
}
© www.soinside.com 2019 - 2024. All rights reserved.