开发者

How can I create a file using apr_file_open()

I开发者_C百科 am making the following call to the Apache Portable Runtime library (version 1.4):

result = apr_file_open(
    &file, // new file handle
    pathname, // file name          
    APR_FOPEN_CREATE | // create file if not there 
    APR_FOPEN_EXCL | // error if file was there already
    APR_FOPEN_APPEND | // move to end of file on open
    APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
    APR_FOPEN_XTHREAD | // allow multiple threads to use file
    0, // flags
    APR_OS_DEFAULT |
    0, // permissions
    pool // memory pool to use
);

if ( APR_SUCCESS != result ) {
    fprintf(
        stderr,
        "could not create file \"%s\": %s",
        pathname,
        apr_errorstr( result )
    );
}

and pathname contains the string /tmp/tempfile20110614091201.

I keep getting the error "Permission denied" (result code APR_EACCES) but I have permissions to read/write to /tmp - what could be causing this?


I needed the APR_FOPEN_WRITE flag. I had mistakenly assumed the APR_FOPEN_APPEND flag was enough.

So, the call that worked was:


    result = apr_file_open(
        &file, // new file handle
        pathname, // file name          
        APR_FOPEN_CREATE | // create file if not there 
        APR_FOPEN_EXCL | // error if file was there already
        APR_FOPEN_WRITE | // open for writing
        APR_FOPEN_APPEND | // move to end of file on open
        APR_FOPEN_BINARY | // binary mode (ignored on UNIX)
        APR_FOPEN_XTHREAD | // allow multiple threads to use file
        0, // flags
        APR_OS_DEFAULT |
        0, // permissions
        pool // memory pool to use
    );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜