


unix/Linux低級(jí)IO函數(shù)的用法
unix/Linux 低級(jí)IO函數(shù)的用法,必須掌握
寫module ,必要的應(yīng)用編程知識(shí)是應(yīng)該要有的, 否則 ,怎么寫應(yīng)用程序來測(cè)試你的module或者driver呢?
而且最主要的是 , 了解上層調(diào)用的接口, 你就明白了上層的開發(fā)者需要什么, 自己才好實(shí)現(xiàn)什么。
把手里的看書的事情都放下, 先把 Unix環(huán)境高級(jí)編程這本書 的第三章 徹底搞熟它
內(nèi)容包括:
open() ,尤其是各種常見的參數(shù),到底是什么意思, 比如常用創(chuàng)建一個(gè)空文件: fd = open("/tmp/xx.txt",O_RDWR | O_CREAT | O_TRUNC);
讀一個(gè)文件,fd = open("/dev/hello",O_RDONLY );
如果就是要寫一個(gè)既有的文件,fd = open("/dev/hello",O_WRONLY );
具體的參數(shù): man 2 open
read(),
write()
ioctl() ,這個(gè)最重要。
lseek() , 文件指針移動(dòng)
比如下面的例子:
就是簡(jiǎn)單的讀一個(gè)文件:
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
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;
}
下面這個(gè)就是簡(jiǎn)單的寫一個(gè)文件 ,
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
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;
}
關(guān)鍵字:Linux、IO函數(shù)、高級(jí)編程
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗(yàn)證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機(jī)IP講解
- CentOS7使用hostapd實(shí)現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯(cuò)
- 解決Centos7雙系統(tǒng)后丟失windows啟動(dòng)項(xiàng)
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解