apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Alex.Krumm-Hel...@csiro.au>
Subject problem appending to a file
Date Thu, 05 Jan 2006 01:38:06 GMT
Hi,

 

I am trying to do some file I/O. The scenario I am aiming for is to
create a file if it doesn't exist, append to it if it exists as well as
allowing for writes to the file from multiple threads. Yet when I use
this combination of options in apr_file_open it always overwrites the
file rather than appending to it. If I remove the multiple thread option
from the open call, it appends correctly. Is this a bug or am I doing
something wrong? Test case follows.

 

 

#include <string>

#include "apr_file_io.h"

 

int main(int argc, char* argv[])

{

            if (apr_initialize() != APR_SUCCESS) 

            {

        return -1;

            }

 

            apr_pool_t *context;

 

            if (apr_pool_create(&context, NULL) != APR_SUCCESS) 

            {

        fprintf(stderr, "Couldn't allocate context.");

        exit(-1);

            }

 

            apr_file_t *logfile;

            apr_status_t openstatus = apr_file_open(&logfile,
"test.txt", APR_APPEND | APR_CREATE | APR_WRITE | APR_XTHREAD,
APR_OS_DEFAULT, context);

            if (openstatus != APR_SUCCESS)

            {

                        return -1;

            }

 

            std::string output("String 1");

            apr_size_t written;

            apr_status_t writestatus = apr_file_write_full(logfile,
output.c_str(), output.length(), &written);

            if (writestatus != APR_SUCCESS || written !=
output.length())

            {

            }

 

            apr_file_flush(logfile);

            if (apr_file_close(logfile) != APR_SUCCESS)

            {

                        return -1;           

            }

            logfile = NULL;

 

            openstatus = apr_file_open(&logfile, "test.txt", APR_APPEND
| APR_CREATE | APR_WRITE | APR_XTHREAD, APR_OS_DEFAULT, context);

            if (openstatus != APR_SUCCESS)

            {

                        return -1;

            }

 

            output.assign("String 2");

            written;

            writestatus = apr_file_write_full(logfile, output.c_str(),
output.length(), &written);

            if (writestatus != APR_SUCCESS || written !=
output.length())

            {

            }

 

            apr_file_flush(logfile);

            if (apr_file_close(logfile) != APR_SUCCESS)

            {

                        return -1;           

            }

            logfile = NULL;

 

            apr_pool_destroy(context);

 

            apr_terminate();

 

            return 0;

}

 

 

---------------------------------------------------------

 Alex.Krumm-Heller@csiro.au   Centie www.centie.net.au

 Tel: +61 2 9372 4657         CSIRO ICT Centre 

 Fax: +61 2 9372 4490         Marsfield, NSW, Australia

---------------------------------------------------------

 


Mime
View raw message