c - linux编程: write to device file

标签 c linux

我是这样写的:

#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/

以及更多关于错误的信息 http://www.java-samples.com/showtutorial.php?tutorialid=597

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

相关文章:

c++ - 具有未声明/未定义类型的 typedef 结构

python - 从 python 在后台运行 "linux" sleep

linux - 使用poll()在命名管道上使用O_RDWR

c++ - 句柄 OpenThread() 已返回,QueueUserApc() 认为无效

c - 不将空格识别为循环中的字符?

c - 算术 double 表达式和 c/c++

c - 当 union 位于 c 结构体内部时,sizeof() 将返回什么?

linux - 从/proc/[pid]/pagemap获取物理地址

linux - 使用grep获取行数

c - 如何在 C 中正确显示 groupID (GID)?