温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

unix/Linux低级IO函数怎么用

发布时间:2021-12-04 15:53:55 来源:亿速云 阅读:99 作者:小新 栏目:系统运维

这篇文章主要介绍unix/Linux低级IO函数怎么用,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

unix/Linux 低级IO函数的用法,必须掌握写module ,必要的应用编程知识是应该要有的, 否则 ,怎么写应用程序来测试你的module或者driver呢?

而且最主要的是 , 了解上层调用的接口, 你就明白了上层的开发者需要什么, 自己才好实现什么。

把手里的看书的事情都放下, 先把 Unix环境高级编程这本书 的第三章 彻底搞熟它。

内容包括:

open() ,尤其是各种常见的参数,到底是什么意思, 比如常用创建一个空文件: fd = open("/tmp/xx.txt",O_RDWR | O_CREAT | O_TRUNC);

读一个文件,fd = open("/dev/hello",O_RDONLY );

如果就是要写一个既有的文件,fd = open("/dev/hello",O_WRONLY );

具体的参数: man 2 open

read(),

write()

ioctl()  ,这个最重要。

lseek() , 文件指针移动

比如下面的例子:

就是简单的读一个文件:

#include

#include

#include

#include

int main(int argc,char **argv)

{

int fd = 0;

int pid = 0;

char buffer[20] = {'\0'};

char *read_buffer[20] = {'\0'};

//fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);

fd = open("/dev/hello",O_RDONLY ); //| O_NONBLOCK);

printf("fd=%d\n",fd);

if(fd < 0) {

perror("/dev/hello");

return -1;

}

read(fd,read_buffer,sizeof(read_buffer)-1);

printf("read_buffer=%s\n",read_buffer);

close(fd);

return 0;

}

下面这个就是简单的写一个文件 ,

#include

#include

#include

#include

int main(int argc,char **argv)

{

int fd = 0;

int pid = 0;

char buffer[20] = {'\0'};

char write_buffer[20] = {'\0'};

strcpy(write_buffer,"zhanglinbao");

fd = open("/dev/hello",O_RDWR | O_CREAT | O_TRUNC);

//fd = open("/dev/hello",O_RDONLY);

printf("fd=%d\n",fd);

if(fd < 0) {

perror("/dev/hello");

return -1;

}

write(fd,write_buffer,sizeof(write_buffer)-1);

close(fd);

return 0;

}

以上是“unix/Linux低级IO函数怎么用”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI