C 语言中的Get_next_line不会在文件末尾停止

Get_next_line in C not stopping at the end of the file

提问人:hélène 提问时间:11/15/2023 最后编辑:hélène 更新时间:11/15/2023 访问量:74

问:

我知道有很多关于这个特定问题的话题,但我似乎找不到我的确切问题。我正在尝试实现具有 42 范数的 get next line 函数。

我已经更改了我的函数 3 天,但没有找到合适的解决方案让我的 get_next_line 函数返回 NULL,一旦它到达文件末尾(即使没有换行),但如果“行”有东西,仍然返回一个字符串。我试图找到一种方法,当重新调用蜂鸣时循环停止,但似乎没有任何效果。有人有想法吗?这是我的代码,提前致谢:

    #include <unistd.h>
#include "get_next_line.h"
#include <stdio.h>

char    *ft_strdup(const char *str1)
{
    char    *str2;
    int     i;

    i = 0;
    str2 = (char *) malloc (ft_strlen(str1) + 1);
    if (str2 == NULL)
        return (NULL);
    while (str1[i])
    {
        str2[i] = str1[i];
        i++;
    }
    str2[i] = 0;
    return (str2);
}

char    *set_line(char *stash, char *line)
{
    int i;
    int j;

    i = ft_strchr_line(stash, '\n');
    j = ft_strlen(stash);
    if (i >= 0)
        line = ft_substr(stash, 0, i + 1);
    else
        line = ft_substr(stash, 0, j);
    return (line);
}

int ft_strchr_line(const char *line, int c)
{
    int i;

    i = 0;
    while (line && line[i])
    {
        if (line[i] == (char) c)
            return (i);
        i++;
    }
    if (!line || line[i] == 0)
        return (-2);
    return (-1);
}

char    *fill_line_buffer(int fd, char *stock, char *buffer)
{
    int i;
    int control;

    i = 1;
    control = 0;
    while (i > 0)
    {
        i = read(fd, buffer, BUFFER_SIZE);
        if (i == -1)
            return (NULL);
        buffer[i] = 0;
        stock = ft_strjoin(stock, buffer);
        control = ft_strchr_line(stock, '\n');
        if ((i == 0 && control == 0) || stock == NULL)
        {
            free(stock);
            return (NULL);
        }
        if (control > 0 || (i == 0 && control == -2))
            return (stock);
    }
    return (stock);
}

char    *get_next_line(int fd)
{
    char        buffer[BUFFER_SIZE + 1];
    static char *stock;
    char        *line;

    line = NULL;
    if (fd == -1)
        return (NULL);
    stock = fill_line_buffer(fd, stock, buffer);
    if (stock == NULL)
        return (NULL);
    line = set_line(stock, line);
    stock = set_stock(stock);
    return (line);
}
#include <fcntl.h>
#include <stdio.h>
int main(void)
{
    int fd;
    char    *line;
    fd = open("text_short.txt", O_RDONLY);
    while ((line = get_next_line(fd)) != NULL)
    {
        printf("%s", line);
        free(line);
    }
    return (0);
}
char    *set_stock(char *stock)
{
    int     i;
    int     j;
    char    *new_stock;

    i = ft_strchr_line(stock, '\n');
    j = ft_strlen(stock);
    if (i >= 0)
        new_stock = ft_substr(stock, i + 1, j);
    else
        return (stock);
    return (new_stock);
}

static int  s_len(char const *s, unsigned int start, size_t len)
{
    size_t  s_len;

    s_len = ft_strlen(s);
    if (start + len > s_len)
        len = s_len - start;
    return (len);
}

char    *ft_substr(char const *s, unsigned int start, size_t len)
{
    unsigned int        i;
    char                *str;

    i = 0;
    len = s_len(s, start, len);
    if (len < 0)
        return ((char *) s);
    if (start > (unsigned int) ft_strlen(s))
    {
        str = malloc(1);
        if (str == NULL)
            return (NULL);
        *str = 0;
        return (str);
    }
    str = malloc(len + 1);
    if (str == NULL)
        return (NULL);
    while (i < len)
    {
        str[i] = s[start + i];
        i++;
    }
    str[i] = 0;
    return (str);
}

char    *ft_strjoin(char *s1, char const *s2)
{
    int     i;
    int     j;
    char    *str3;

    i = 0;
    j = 0;
    str3 = (char *)malloc(ft_strlen(s1) + ft_strlen(s2) + 1);
    if (str3 == NULL)
        return (NULL);
    while (s1 && s1[i])
        str3[j++] = s1[i++];
    i = 0;
    while (s2 && s2[i])
        str3[j++] = s2[i++];
    str3[j] = 0;
    s1 = ft_strdup(str3);
    free(str3);
    if (s1 == NULL)
        return (NULL);
    return (s1);
}

int ft_strlen(const char *s)
{
    int i;

    i = 0;
    while (s && s[i])
        i++;
    return (i);
}

添加了 :我们必须做什么的描述:

  • 列表项

“函数名称:get_next_line

  • 列表项 原型:char *get_next_line(int fd);
  • 列表项

上交文件:get_next_line.c、get_next_line_utils.c、get_next_line.h

  • 列表项

参数 fd:要从中读取的文件描述符

  • 列表项

返回值读取行:正确行为 NULL:没有其他内容可读取,或发生错误

  • 列表项

外部 functs.read、malloc、free

  • 列表项

描述 编写一个函数,返回从文件描述符读取的一行 • 重复调用(例如,使用循环)到 您的 get_next_line() 函数应该允许您读取文件描述符指向的文本文件,一次一行。函数应返回已读取的行。如果没有其他可读取的内容,或者如果发生错误,则应返回 NULL。• 确保函数在读取文件和从标准输入读取时都按预期工作。• 请注意,返回的行应包含终止 \n 字符,除非已到达文件末尾且不以 \n 字符结尾。您将按如下方式编译代码(以缓冲区大小 42 为例):cc -Wall> -Wextra -Werror -D BUFFER_SIZE=42 .c

C getline

评论

1赞 Some programmer dude 11/15/2023
您是否尝试过使用调试器逐行执行代码,同时监视变量及其值,以查看实际情况?
0赞 Scott Hunter 11/15/2023
也许记录一下你期望它如何工作?
0赞 user3386109 11/15/2023
“有42规范”???那是什么意思?
2赞 n. m. could be an AI 11/15/2023
你把情况大大复杂化了。循环逐个读取字符。看?返回字符串。看到 EOF?返回 NULL(或任何您想要的内容)。\n
2赞 Barmar 11/15/2023
@Shawn我一直想知道前缀是从哪里来的。ft_

答: 暂无答案