/* * 1-1.c * * Created on: 2010-12-29 * Author: jinyong * 文件的打开和关闭 */ #include <stdio.h>//标准输入输出库 #include <stdlib.h>//包含系统函数库 #include <fcntl.h>//包含open函数库 int main(void) { int fd; if( (fd=open("/home/jinyong/4-6file",O_CREAT|O_TRUNC|O_WRONLY,0600)) < 0 ) { /** * int open(const char *pathname,int flags); * int open(const char *pathname,int flags,mode_t mode); * 打开或新建文件 * pathname为文件的访问路径 * flags为访问文件的命令模式 * mode为设置新增文件的权限 * 正确返回0,错误返回-1 * * flags列表: * O_RDONLY 只读 * O_WRONLY 只写 * O_RDWR 读写 * O_APPEND 追加 * O_TRUNG 舍弃现有文件数据,即覆盖 * O_CREAT 建立文件,可使用mode参数设置新建文件权限 * O_EXCL 与O_CREAT一起使用,若所建立的文件已存在,则打开失败。 */ //选项O_TRUNC表示文件存在时清空 perror("打开文件出错!"); exit(1); } else { printf("打开文件4-6file,文件描述符为:%d/n",fd); } if(close(fd) < 0) { perror("关闭文件错误"); exit(1); } system("ls /home/jinyong/4-6file -l"); return EXIT_SUCCESS; }