gpt4 book ai didi

c - 读取到 procfile 末尾时遇到问题

转载 作者:行者123 更新时间:2023-11-30 17:45:53 24 4
gpt4 key购买 nike

我在读取大于缓冲区的 proc 文件时遇到问题。我看过这两个问题,我想我遗漏了一些东西。我是否错误地使用了 buffer_location 指针?

How can I read large data from proc file?

how read to the end of /proc file

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

//this function reads a data structure and fills the char* messages
read_log();

if( strlen(messages) < buffer_length ) {
//if the string is less than the size of the buffer read it all
memcpy(buffer, messages, strlen(messages)+1);
ret = strlen(messages)+1;
kfree(messages);
}
else {
//read just the buffer_length
memcpy(buffer, messages, buffer_length);

//move the messages char * up for the next read
char *temp = kmalloc(strlen(messages)-buffer_length+1, GFP_KERNEL);
strcpy(temp, messages+buffer_length);
kfree(messages);
messages = temp;

//from the question linked above I am putting my first lump of data
//into the buffer, setting *buffer_location = buffer, and returning buffer_length
*buffer_location = buffer;
ret = buffer_length;
}
return ret;
}

但是我的 procfile_read 没有再次被调用,就像我想的那样。我做错了什么?

最佳答案

您忘记设置 eof 返回参数。

*eof = 0;
return ret;

当然,直到您没有更多数据要发送,在这种情况下设置*eof = 1;

关于c - 读取到 procfile 末尾时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19531228/

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