Whoops, I forgot to mention that this is occurring under windows, whereas it works fine under linux.

 

Alex

 

-----Original Message-----
From: Krumm-Heller, Alex (ICT Centre, Marsfield)
Sent: Thursday, 5 January 2006 12:38 PM
To: dev@apr.apache.org
Subject: problem appending to a file

 

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

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