简体   繁体   English

如何创建 (C) 函数以使用“读取”将文件中的数据读取到链表中?

[英]How can I create a (C) function to read data from a file into a linked-list using 'read'?

I am trying to create a function that reads all characters in a given file into a linked-list, returning a pointer to the list.我正在尝试创建一个函数,该函数将给定文件中的所有字符读入链表,并返回指向该列表的指针。 I cannot use fopen, fread, or fclose.我无法使用 fopen、fread 或 fclose。 I have confirmed that this is working code if operating on a string:如果对字符串进行操作,我已经确认这是工作代码:

Note that ft_lstnew creates a link with the content as the first peram, returning a pointer to that link.请注意,ft_lstnew 创建一个包含内容的链接作为第一个 peram,返回指向该链接的指针。

head = ft_lstnew(&str[i++], 1);
curr = head;
while(str[i])
{
    curr->next = ft_lstnew(&str[i++], 1);
    curr = curr->next;
}
curr->next = ft_lstnew(&str[i], 1);

How can I alter this code to operate on character in a file instead of in a string using the read function?我如何更改此代码以使用读取函数对文件中的字符而不是字符串中的字符进行操作?

My Attempt我的尝试

t_list *ft_lstnew(void const *content, size_t content_size);

t_list *read_tetriminos(char *file)
{
    int fd;
    char c;
    t_list *curr;
    t_list *head;

    fd = open(file, O_RDONLY, 0);
    read(fd, &c, 1);
    head = ft_lstnew(&c, 1);
    curr = head;
    while(curr->content != EOF)
    {
        read(fd, &c, 1);
        curr->next = ft_lstnew(&c, 1);
        curr = curr->next;
    }
    close(fd);
    return(head);
}

You're almost there.您快到了。 The only thing that you are doing wrong is assuming that read will write EOF inside the provided buffer when reaching the end of the file.您做错的唯一一件事是假设read到达文件末尾时会将EOF写入提供的缓冲区内。 EOF is just a special value used by higher level functions provided by stdio.h to signal the end of a FILE object has been reached. EOF只是一个特殊值,由stdio.h提供的更高级别的函数用来表示已到达FILE对象的末尾。 Furthermore, EOF is not a char , but a int .此外, EOF不是char ,而是int Here you're using raw syscalls such as open and read to accomplish your task, and EOF has nothing to do with those.在这里,您使用原始系统调用(例如openread )来完成您的任务,而EOF与这些无关。

You can take a look at the manual page for read to see what happens when the end of the file is reached:您可以查看read的手册页,了解到达文件末尾时会发生什么:

ssize_t read(int fd, void *buf, size_t count);

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf . read()尝试从文件描述符fd中读取最多count个字节到从buf开始的缓冲区中。

On files that support seeking, the read operation commences at the file offset, and the file offset is incremented by the number of bytes read.在支持查找的文件上,读取操作从文件偏移量开始,文件偏移量按读取的字节数递增。 If the file offset is at or past the end of file, no bytes are read, and read() returns zero.如果文件偏移量位于或超过文件末尾,则不会读取任何字节,并且read()返回零。

Therefore, your program can be re-written like this (I also added some error checking):因此,您的程序可以这样重写(我还添加了一些错误检查):

t_list *read_tetriminos(char *file)
{
    int fd;
    ssize_t nread;
    char c;
    t_list *curr;
    t_list *head;

    fd = open(file, O_RDONLY);
    if (fd == -1) {
        // Open failed.
        return NULL;
    }

    nread = read(fd, &c, 1);
    if (nread == -1) {
        // Read error.
        return NULL;
    } else if (nread == 0) {
        // File is empty.
        return NULL;
    }

    head = ft_lstnew(&c, 1);
    curr = head;

    while (read(fd, &c, 1) == 1) // You can check for errors here too, this is just simplified.
    {
        curr->next = ft_lstnew(&c, 1);
        curr = curr->next;
    }

    close(fd);
    return head;
}

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

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