comp.lang.ada
 help / color / mirror / Atom feed
From: Jacob Sparre Andersen <sparre@nbi.dk>
Subject: Re: Opening a file vs. creating if not yet exists
Date: 19 Aug 2008 16:23:49 +0200
Date: 2008-08-19T16:23:49+02:00	[thread overview]
Message-ID: <87k5edjd6y.fsf@nbi.dk> (raw)
In-Reply-To: af68d80f-0f42-4524-a963-80c91b427ad9@k7g2000hsd.googlegroups.com

Maciej Sobczak wrote:

> I find this a bit annoying for example with log files, where the
> natural scheme should be:
> - if the file exists, open and append to it
> - if the file does not exist, create it and append to it
> 
> What is the recommended way to deal with this?
> 
> begin
>    Open (File, Append_File, "file.txt");
> exception
>    when Name_Error =>
>       Create (File, Append_File, "file.txt");
> end;
> 
> Is the above idiomatic (ie. well understood and accepted as common
> practice)?

I am certain it is well understood and commonly used, but in general
it is not advised to use exceptions in ordinary control flow.  Amado's
suggestion using Ada.Directories.File_Exists is definitely closer to
the advised practice.

The problem with both of these solutions is that they are not atomic.
POSIX.IO.Open_Or_Create is more appropriate (if you are targeting a
standards compliant operating system), since it solves the problem as
an atomic operation.

Greetings,

Jacob
-- 
"Any, sufficiently advanced, technology is indistinguishable from magic."



  parent reply	other threads:[~2008-08-19 14:23 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-08-18 15:03 Opening a file vs. creating if not yet exists Maciej Sobczak
2008-08-19 12:36 ` Rob Norris
2008-08-19 12:58 ` amado.alves
2008-08-19 14:23 ` Jacob Sparre Andersen [this message]
2008-08-19 14:58   ` Maciej Sobczak
2008-08-21 15:41     ` Martin Krischik
2008-08-21 21:06       ` Maciej Sobczak
2008-08-21 15:42     ` Martin Krischik
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox