原型
文件描述符、套接字等打开函数。
打开文件返回的文件描述符的值为3,因为0(标准输入)、1(标准输出)、2、(标准错误)文件描述符在程序启动的时候默认分配的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
int open(const char* path, int flags) int open(const char* path, int flags, mode_t mode);
|
使用参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>
#include <stdlib.h> #include <stdio.h> #include <errno.h> #include <string.h>
#define ERR_EXIT(m)\ do \ {\ perror(m);\ exit(EXIT_FAILURE);\ }while(0)
int main() { umask(0); int fd; fd = open("test.txt", O_WRONLY | O_CREAT, 0666);
if(fd == -1) ERR_EXIT("open error");
printf("open success\n"); close(fd); return 0; }
|