首页 / LINUX / linux编程:写入设备文件
linux编程:写入设备文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux编程:写入设备文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1181字,纯文字阅读大概需要2分钟。
内容图文
我写了这个:
#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
如果我的程序有任何错误?
解决方法:
是的,有一个问题.你使用perror()是错误的.
在调用perror之前,您应首先检查系统调用是否表示存在问题.
手册页非常清楚:
Note that errno is undefined after a successful library call: this call
may well change this variable, even though it succeeds, for example
because it internally used some other library function that failed.
Thus, if a failing call is not immediately followed by a call to per‐
ror(), the value of errno should be saved.
您应该检查每个系统的返回代码,并且只有在它们失败时才调用perror.
像这样的东西:
fd = open("/dev/mtd0", O_RDWR);
if (fd < 0) {
perror("open: ");
return 1;
}
if (lseek(fd, 1, SEEK_SET) < 0) {
perror("lseek: ");
return 1;
}
if (write(fd, &buf, 4) < 0) {
perror("write: ");
return 1;
}
close(fd);
内容总结
以上是互联网集市为您收集整理的linux编程:写入设备文件全部内容,希望文章能够帮你解决linux编程:写入设备文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。