gpt4 book ai didi

c - 如何读到/proc 文件的末尾

转载 作者:太空宇宙 更新时间:2023-11-04 03:02:13 24 4
gpt4 key购买 nike

我有/proc 文件。我不明白 read 函数的一些论点。阅读功能看起来像这样:

int procfile_read(char *buffer,  char **buffer_location,  off_t offset, int buffer_length, int *eof, void *data)

我不知道如何使用偏移参数。

一些例子使用这样的偏移量参数:

if (offset > 0)
return 0;

他们解释说:这很重要,因为库中的标准读取函数将继续发出读取系统调用,直到内核回复它没有更多信息,或者直到其缓冲区已满。

我有一个大于缓冲区的文件。我怎样才能读到文件末尾???

最佳答案

参见 proc_file_read功能实现,尤其是注释:

  74                        /*
75 * How to be a proc read function
76 * ------------------------------
77 * Prototype:
78 * int f(char *buffer, char **start, off_t offset,
79 * int count, int *peof, void *dat)
80 *
81 * Assume that the buffer is "count" bytes in size.
82 *
83 * If you know you have supplied all the data you
84 * have, set *peof.
85 *
86 * You have three ways to return data:
87 * 0) Leave *start = NULL. (This is the default.)
88 * Put the data of the requested offset at that
89 * offset within the buffer. Return the number (n)
90 * of bytes there are from the beginning of the
91 * buffer up to the last byte of data. If the
92 * number of supplied bytes (= n - offset) is
93 * greater than zero and you didn't signal eof
94 * and the reader is prepared to take more data
95 * you will be called again with the requested
96 * offset advanced by the number of bytes
97 * absorbed. This interface is useful for files
98 * no larger than the buffer.
99 * 1) Set *start = an unsigned long value less than
100 * the buffer address but greater than zero.
101 * Put the data of the requested offset at the
102 * beginning of the buffer. Return the number of
103 * bytes of data placed there. If this number is
104 * greater than zero and you didn't signal eof
105 * and the reader is prepared to take more data
106 * you will be called again with the requested
107 * offset advanced by *start. This interface is
108 * useful when you have a large file consisting
109 * of a series of blocks which you want to count
110 * and return as wholes.
111 * (Hack by Paul.Russell@rustcorp.com.au)
112 * 2) Set *start = an address within the buffer.
113 * Put the data of the requested offset at *start.
114 * Return the number of bytes of data placed there.
115 * If this number is greater than zero and you
116 * didn't signal eof and the reader is prepared to
117 * take more data you will be called again with the
118 * requested offset advanced by the number of bytes
119 * absorbed.
120 */

关于c - 如何读到/proc 文件的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10155329/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com