首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Unix环境高级编程札记:3、文件I/O

2013-11-15 
Unix环境高级编程笔记:3、文件I/O#include stdio.h#include fcntl.h#include unistd.h#include stdl

Unix环境高级编程笔记:3、文件I/O
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <stdlib.h>?int main(int argc, char **argv) {int val;if(argc !=2) {printf("argc !=2\n");exit(1);}if((val = fcntl(atoi(argv[1]),F_GETFL,0)) < 0) {printf("fcntl error");exit(1);}?switch (val & O_ACCMODE) {case O_RDONLY:printf("read only");break;case O_WRONLY:printf("write only");break;case O_RDWR:printf("read write");break;default:printf("unknow acces mode");}if(val & O_APPEND)printf("append");if(val & O_NONBLOCK)printf("nonblocking");#if defined (O_SYNC)if(val & O_SYNC)printf("synchronous");#endifputchar('\n');exit(0);}???12、ioctl? ? ioct函数是io操作的杂物箱,不能使用本中其他函数表示的io操作通用都能用ioctl表示。终端io是ioctl的最大使用方面。?13、/dev/fd? ? /dev/fd目录,其目录项是名为0、1、2等文件,打开文件/dev/fd/n 等效于复制描述符n? ? fd = open("/dev/fd/0",mode);? ? 等效于fd = dup(0)? ? 所以描述符0和fd共享同一文件表项,例如,描述符0先前被打开为只读,那么我们也只能对fd进行只读操作

?

热点排行