日期:2014-05-16  浏览次数:20621 次

请问以 0|O_CREAT|O_EXCL ,方式建立的文件, 再次打开时, 为何返回失败呢 ? 谢谢 。

代码如下 

C/C++ code

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <limits.h>

int main( int arg_num, char *arg[] )
{
//    int fd = open("/home/dm/exercise/file1d2",0|O_CREAT|O_EXCL);
    int fd = open(*arg, 0);
    int max = sysconf(_SC_MQ_OPEN_MAX);
    max = sysconf(_SC_OPEN_MAX);
    if (fd == -1)
    {
        printf("open fail  \n");
        return 1;
    }

    printf("open ok %d", fd);
    printf("\n\n");
    return 0;
}
//int open (const char *name, int flags);
//int open (const char *name, int flags, mode_t mode);




------解决方案--------------------
int fd = open("/home/dm/exercise/file1d2",0|O_CREAT|O_EXCL);

第一次创建成功结果没有指定mode,再打开也没有权限了。