首页 > 其他 > 详细

04文件与IO

时间:2015-03-23 01:37:30      阅读:300      评论:0      收藏:0      [点我收藏+]

文件系统调用:

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;
}

 

04文件与IO

原文:http://www.cnblogs.com/DamonBlog/p/4358551.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!