/*
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 }
linux系统I\O文件 open()函数使用
最新推荐文章于 2024-05-21 20:56:54 发布