linux系统I\O文件 open()函数使用

 /*
  2 ##open()函数,头文件:#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
  3 ##原型:
  4 int open(const char *pathname, int flags);
  5 int open(const char *pathname, int flags, mode_t mode);
  6 int creat(const char *pathname, mode_t mode);
  7 ##flags参数:
  8 O_RDONLY        以只读方式打开
  9 O_WRONLY        以只写方式打开
 10 O_RDWR  以读写方式打开
 11 O_APPEND        把写入数据追加在文件的末尾
 12 O_CREAT 如果需要,就按参数mode给出的访问模式创建文件   文件权限= mode&(取反的umask)
 13 O_EXCL  与O_CREAT一起使用,确保调用者创建出文件。open是一个原子操作,
      也就是说,它只执行一个函数调用。使用这个可选模式可以防止两个程序同时创建
      一个文件,如果文件在,open调用将失败 。
 14 ##pathname:     路径名称
 15 
 16 ##close()函数,头文件:#include <unistd.h>
 17 ##原型:
 18 int close(int fd)
 19 ##返回值:0表示成功,-1表示错误
 20 */
 21 #include<sys/types.h>
 22 #include<sys/stat.h>
 23 #include<fcntl.h>
 24 #include<unistd.h>
 25 #include<stdio.h>
 26 #include<stdlib.h>
 27 void main(void)
 28 {
 29         int fn;
 30         char * pn = malloc(sizeof(char * )*5);
 31         scanf("%s",pn);
 32         if(fn=open(pn,O_CREAT|O_RDWR|O_EXCL)==-1){
 33                 perror("open fail");
 34         }
 35         else{
 36                 printf("sussecful!!");
 37                 close(fn);
 38         }
 39 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值