我是这样写的:

#include <stdio.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <mtd/mtd-user.h>
#include <errno.h>

int main( void )
{
        int fd;
        char buf[4]="abc";

        fd = open("/dev/mtd0", O_RDWR);
        lseek(fd, 1, SEEK_SET);
        write(fd, &buf, 4);
        close(fd);
        perror("perror output:");

        return 0;
}

文件/dev/mtd0 是使用 nandsim 内核模块创建的,然后运行
mtdinfo /dev/mtd0

得到了有意义的输出。在我运行我的程序之后,它的输出是:
perror output:: Invalid argument

如果我的程序有任何错误?

最佳答案

你应该有这样的东西

if(-1 == write(fd, &buf, 4)){
  perror("perror output:");
}
close(fd);

因为 perror 显示最后一个错误。

http://www.cplusplus.com/reference/clibrary/cstdio/perror/

以及更多关于 perror http://www.java-samples.com/showtutorial.php?tutorialid=597

关于c - linux 编程 : write to device file,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10362111/

10-16 17:02