inputstream - Linux 设备驱动程序调用时计数错误

标签 inputstream linux-device-driver iostream

我在CentOS 7上实现了一个字符设备驱动程序。当从C程序调用时,该驱动程序运行正常,因此...

char bytes[8];
int fd = open("/dev/fortuna", O_RDONLY);
if (fd < 0) {
    perror("File open error: ");
    return -1;
}
int in = read(fd, bytes, 8);
if (in < 0) {
    perror("File read error: ");
}
close(fd);

在 count = 8 的情况下调用驱动程序读取函数,并且程序完成且没有错误。驱动函数原型(prototype)是...

static ssize_t fortuna_read(struct file *filp, char *buff, size_t count, loff_t *offp);

但是,如果从 C++ 流调用读取,如下所示...

char bytes[8];
std::ifstream in("/dev/fortuna");
if (in.good()) {
    in.read(bytes, 8);
}
in.close();

使用 count = 8191 调用驱动程序读取函数。如果从 Java FileReader 调用驱动程序,如下所示...

File file = new File("/dev/fortuna");
file.setReadOnly();
FileReader reader = new FileReader(file);
char[] cbuf = new char[8];
int read = reader.read(cbuf);
reader.close();

驱动程序读取函数是通过 count = 8192 调用的。写入函数的行为类似。

谷歌让我失望了。帮忙?

最佳答案

这与缓冲有关。 我很确定如果您一直使用 stdio.h 和 fopen() I/O 系列而不是 open(),您会在 C 中看到同样的问题。

所以你应该禁用缓冲区。 对于 C++ 有这样的答案: How to disable buffering on a stream?

关于inputstream - Linux 设备驱动程序调用时计数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39442523/

相关文章:

Android:使用 InputStream 打开位于 SD 中的文件时出现问题

java - 从 .jar 文件夹中读取文件

java - 我怎么知道哪个文件流支持在 Java 中查找

linux - pm_runtime_put_sync() 函数的功能是什么?

linux - 中断处理和用户空间通知

c++ - 自定义数据 iostream

java - 该方法必须返回InputStream类型的结果

Linux 内核 IIO 事件 sysfs 文件只可读

c++ - operator>> 是否应该清空一个容器?

c++ - 如何清除 cin 中的无关字符?