页次: 1
文件的开打开使用 open, 文件的关闭使用 close
函数的原型可以看 linux 系统文件函数总结
示例
#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> #include <unistd.h> int main() { int fd; if((fd = open("not_a_file.txt", O_RDONLY)) == -1) { perror("error_info"); _exit(1); } else { puts("open file success"); close(fd); // 关闭文件 } return 0; }
如果一个文件不存在, 就显示错误的信息
error_info: No such file or directory
文件的打开常用常用哟
O_RDONLY -- 只读的方式打开
O_WRONLY -- 只写方式打开
O_RDWR -- 读写的方式
O_CREAT -- 文件不存在创建一个文件
O_EXCL -- 如果文件存在, 使用 O_CREAT 则出错。
ipbbs.net
离线
页次: 1