2010-01-23 181 views
2

由于某些原因,我无法打开()打开文件。这是我的代码。open()无法打开文件

static int context_ctor(X86Context *ctx) 
{ 
    char file[512]; 
    memset(ctx, 0, sizeof(X86Context)); 

    sprintf(file, "%s.%d", "test", getpid()); 

    ctx->fp = open(file, O_RDWR); 

    if(ctx->fp < 0) { 
     printf("errno %d %s\n", errno, file); 
     return VISUAL_ERROR_GENERAL; 
    } 

    ctx->buf = mmap(0, MAXFILESIZE, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, ctx->fp, 0); 

    printf("context_ctor: %p\n", ctx->buf); 

    close(ctx->fp); 
    exit(0); 
} 

而这里的输出:

errno 2 test.12356 

仰望错误代码显示:

[EACCES] 
    Permission denied. 

我知道我有权读/写/在这个目录下执行文件。我甚至试过/tmp/test.pid。任何想法?

回答

2

如果你想创建你需要使用O_CREAT一个新的文件,所以:

ctx->fp = open(file, O_CREATE | O_RDWR); 

顺便说一句,你可能需要使用字符串错误(错误),以显示你的错误

+0

那得到它了。谢谢。 – Scott 2010-01-23 10:25:50