文件系统调用:
open、close、create、read、write
int open(const char* path, int flags)
path:要打开的文件的路径
flags:打开的模式
执行结果:成功返回文件描述符,失败返回-1。
#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) (perror(m), exit(EXIT_FAILURE)) //这种宏定义的写法更为专业,工作中都是用这种 #define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0) int main(void) { int fd; fd = open("test.txt", O_RDONLY); /* if (fd == -1) { fprintf(stderr, "open error with errno=%d %s\n", errno, strerror(errno)); exit(EXIT_FAILURE); } */ /* if (fd == -1) { perror("open error"); exit(EXIT_FAILURE); } */ if (fd == -1) ERR_EXIT("open error"); printf("open succ\n"); return 0; }
原文:http://www.cnblogs.com/DamonBlog/p/4358551.html