Opening a file vs. creating if not yet exists

This is a discussion on Opening a file vs. creating if not yet exists within the ADA forums in Programming Languages category; Hi, The Open procedure for opening files expects the given external file to already exist (otherwise the Name_Error is signalled). The Create procedure truncates the file no matter what, even if the mode for opening a file is Append_File. 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, ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-18-2008, 11:03 AM
Maciej Sobczak
Guest
 
Default Opening a file vs. creating if not yet exists

Hi,

The Open procedure for opening files expects the given external file
to already exist (otherwise the Name_Error is signalled).

The Create procedure truncates the file no matter what, even if the
mode for opening a file is Append_File.

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)?

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada
Reply With Quote
  #2  
Old 08-19-2008, 08:36 AM
Rob Norris
Guest
 
Default Re: Opening a file vs. creating if not yet exists

On Mon, 18 Aug 2008 08:03:29 -0700 (PDT), Maciej Sobczak <see.my.homepage@gmail.com> wrote:

>
>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 don't know if it's common practice, but that's the main way we handle it for log files.
Reply With Quote
  #3  
Old 08-19-2008, 08:58 AM
amado.alves@gmail.com
Guest
 
Default Re: Opening a file vs. creating if not yet exists

> 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)?


There seems to be two schools of though regarding the use of
exceptions for logic.
Some of us accept the idiom happily.
But probably the most conventional school is the "don't"
So in this particular case and if you are using Ada 2005 then you
might consider the uncontroversial

if Ada.Directories.File_Exists (Filename) then
Open (...)
else
Create (...)
end if;
Reply With Quote
  #4  
Old 08-19-2008, 10:23 AM
Jacob Sparre Andersen
Guest
 
Default Re: Opening a file vs. creating if not yet exists

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."
Reply With Quote
  #5  
Old 08-19-2008, 10:58 AM
Maciej Sobczak
Guest
 
Default Re: Opening a file vs. creating if not yet exists

On 19 Sie, 16:23, Jacob Sparre Andersen <spa...@nbi.dk> wrote:

> 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.


I understand this, but for some reasons I have to stick to Ada 95
features in this particular case.

The atomicity of the complete solution is not an issue, since there is
no chance for the log file to be deleted by some other entity.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada
Reply With Quote
  #6  
Old 08-21-2008, 11:41 AM
Martin Krischik
Guest
 
Default Re: Opening a file vs. creating if not yet exists

Maciej Sobczak schrieb:

> The atomicity of the complete solution is not an issue, since there is
> no chance for the log file to be deleted by some other entity.


Realy no chance? Did you consider

rm -rf *.log

or

del *.log

Ok I am pulling your leg here.

Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
Reply With Quote
  #7  
Old 08-21-2008, 11:42 AM
Martin Krischik
Guest
 
Default Re: Opening a file vs. creating if not yet exists

Maciej Sobczak schrieb:

> The atomicity of the complete solution is not an issue, since there is
> no chance for the log file to be deleted by some other entity.


Realy no chance? Did you consider

rm -rf *.log

or

del *.log

Ok I am pulling your leg here.

Martin
--
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com
Reply With Quote
  #8  
Old 08-21-2008, 05:06 PM
Maciej Sobczak
Guest
 
Default Re: Opening a file vs. creating if not yet exists

On 21 Sie, 17:41, Martin Krischik <krisc...@users.sourceforge.net>
wrote:

> > The atomicity of the complete solution is not an issue, since there is
> > no chance for the log file to be deleted by some other entity.

>
> Realy no chance? Did you consider
>
> rm -rf *.log


Yes, and this is not an issue for the reason of hazards.
Since the action of removing files this way would be asynchronous with
regards to the logging program, we should really protect against
calling it in both cases: "just before" opening the file as well as
"just after". The latter is a problem - no amount of trickery in the
program (atomic or not) can protect this case and if that happens the
log file becomes invisible (except for the program which would keep
the only, but unnamed, link to the file) and the file would vanish at
the program exit. Not very useful.
That's why we have to either assume that this never happens - and then
the first case need no protection - or that it is hopeless anyway.
Nothing to do in either case.

(the above assumes POSIX, Windows may behave differently)

> Ok I am pulling your leg here.


Sure. That's what makes usenet more funny. :-)

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

Database Access Library for Ada: www.inspirel.com/soci-ada
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 09:39 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.