1

我正在研究使用 read() 函数来读取整个数据结构,每个数据结构的类型都与其他数据结构相同,但数据不同,然后将它们放入链表中。出于某种原因,我似乎找不到任何关于如何终止循环的具体信息,其中read(fp, &tmp, sizeof(struct foo))包括new_node(tmp).

我希望能够简单地阅读直到 EOF,但我不知道如何使用 read() 函数确定 EOF 所在的位置。显然,我可以使用 write() 函数的变通方法,在写入之前我将在文件中包含结构的数量,然后在达到该数量时终止读取函数,但这似乎有点笨拙,并且避免了原来的知道文件何时终止的想法。

跟进:

我感谢您的帮助,并且我已经实施了我所看到的。不幸的是,我相信我可能读错了信息。相关代码:

struct test_t{
    int data;
    char buf[LEN];
        struct test_t * next;
};

struct test_t * new_node(struct test_t node, struct test_t * tail)
{
    struct test_t * tmp = NULL;

    if(!(tmp = malloc(sizeof(struct test_t))))
        return NULL;

    tmp->data = node.data;
    strcpy(tmp->buf, node.buf);
    tmp->next = NULL;
    if(tail)
        tail->next = tmp;

    return tmp;
}

...

while(read(fd, &tmp, sizeof(struct test_t)) == sizeof(struct test_t)){
    printf("%d, %s\n", tmp.data, tmp.buf);
    tail = new_node(tmp, tail);
    if(head == NULL)
        head = tail;
    printf("%d, %s\n", tail->data, tail->buf);
}

...

fd = open("test.txt", O_WRONLY | O_CREAT, 0666);
iter = head;
while(iter){
    printf("%d\n", write(fd, &iter, sizeof(struct test_t)));
    printf("%d, %s\n", iter->data, iter->buf);
    iter = iter->next;
}

这是写循环的输出:

112
1, a
112
2, b
112
3, c
112
4, d
112
5, e

该文件以二进制形式保存,但我可以清楚地知道只有尾部似乎被写入了五次。我不确定为什么会这样。

读取循环中诊断 printf 的输出是:

23728144, 
23728144, 
23728272, 
23728272, 
23728400, 
23728400, 
23728528, 
23728528, 
23728656, 
23728656,

输出让我觉得它将下一个指针的值放入数据 int 中。知道为什么:1)我可能连续五次在同一个节点上写()?2)当我阅读()时我变得乱码?

4

4 回答 4

2

忽略错误条件,我认为这是基本思想:

while (read(fp, &tmp, sizeof(struct foo))==sizeof(struct foo))
    new_node(tmp);
于 2013-12-02T00:02:22.513 回答
2
while (read(fd, &tmp, sizeof(tmp)) == sizeof(tmp))
{
    ...got another one...
}

通常使用FILE *fp;and int fd;(因此文件描述符的名称是fdand not fp)。

read()函数返回它读取的字节数。如果没有更多数据,则返回 0。对于磁盘文件等,它将返回请求的字节数(除非最后可能没有那么多字节可读取)或 0 表示没有剩余数据读取(或 -1,如果设备上有错误,而不仅仅是没有更多数据)。对于终端(以及套接字和管道),它将读取尽可能多的可用字节,而不是等待请求的大小(因此每次读取都可能返回不同的大小)。显示的代码仅在读取短读、EOF 或错误时读取完整大小的结构和 baulks。


ensc在他的回答中的代码涵盖了所有实际情况,但不是我编写等效循环的方式。我会使用:

struct foo tmp;
ssize_t nbytes;

while ((nbytes = read(fd, &tmp, sizeof(tmp))) != 0)
{
    if ((size_t)nbytes = sizeof(tmp))
        process(&tmp);
    else if (nbytes < 0 && errno == EINTR)
        continue;
    else if (nbytes > 0)
        err_syserr("Short read of %zu bytes when %zu expected on fd %d\n",
                   nbytes, sizeof(tmp), fd);
    else
        err_syserr("Read failure on fd %d\n", fd);
}

两种正常情况——全长记录读取正常和检测到EOF——在循环顶部处理;深奥的案件在循环中进一步处理。我的err_syserr()函数printf()类似于 - 并报告其参数给出的错误,以及与errno它是否为非零相关的错误,然后退出。您可以使用任何等效机制。我可能会也可能不会将文件描述符编号放在错误消息中;这取决于谁会看到错误。如果我知道文件名,我肯定会将其包含在消息中,而不是文件描述符。

nbytes == -1 && errno == EINTR与@ensc 的评论相反,我认为处理此案没有任何困难。

于 2013-12-01T23:58:56.120 回答
2

read 返回读取的字节数。如果您执行读取,并且返回值小于您请求的字节数,那么您知道它在读取期间达到了 EOF。如果它正好等于请求的字节数,那么文件没有达到 EOF,或者它已经达到,并且文件中正好剩下 0 个字节,在这种情况下,下一次调用 read() 将返回 0。

while(read(fd, &tmp, sizeof(tmp)) > 0) {
    ...
}
于 2013-12-02T00:00:36.850 回答
2
for (;;) {
    struct foo tmp;
    ssize_t l = read(fd, &tmp, sizeof tmp);

    if (l < 0 && errno == EINTR) {
        continue;
    } else if (l < 0) {
        perror("read()");
        abort();
    } else if (l == 0) {
        break;   /* eof condition */
    } else if ((size_t)(l) != sizeof tmp) {
        abort(); /* something odd happened */
    } else {
        handle(&tmp);
    }
}

编辑:

在我的项目中,我使用通用

bool read_all(int fd, void *dst_, size_t len, bool *is_err)
{
        unsigned char *dst = dst_;

        *is_err = false;

        while (len > 0) {
                ssize_t l = read(fd, dst, len);

                if (l > 0) {
                        dst += l;
                        len -= l;
                } else if (l == 0) {
                        com_err("read_all", 0, "read(): EOF");
                        *is_err = (void *)dst != dst_;
                        break;
                } else if (errno == EINTR) {
                        continue;
                } else {
                        com_err("read_all", errno, "read()");
                        *is_err = true;
                        break;
                }
        }

        return len == 0;
}

功能。因为我更喜欢说要读取多少元素的方法,所以EOF这里将 an 作为错误处理。但是将另一个bool *err参数添加到在非 EOF 错误情况下设置的函数将是微不足道的。 您可以将上面用作

while (read_all(fd, &tmp, sizeof tmp, &is_err))
    new_node(&tmp);
于 2013-12-02T00:01:41.540 回答