我正在学习与用户级程序通信的内核模块,最初是用
FILE *pFile = fopen(...)然后给它写信
char *str = malloc(10);
fputs(str, pFile);然而,现在我需要使用#include <fcntl.h>中的功能来尝试一些异步通知,而我的示例则使用
int pFile;
pFile = open("/dev/mymodule", O_RDWR);我认为我不能再使用fputs()或fgets()了,因为它需要一个FILE *ptr,而现在我有一个int。我想我应该用write()和read()代替。
它就是这样运作的吗?
write(pFile, str, sizeof(str));
char line[256];
while (read(pFile, line, 256) != NULL) {
printf("%s", line);
}发布于 2016-03-18 15:53:47
可以使用FILE*基于文件描述符打开fdopen()。
FILE *file = fdopen(pfile, "rw");参考资料:fdopen - associate a stream with a file descriptor
https://stackoverflow.com/questions/36088757
复制相似问题