| Register | FAQ | Calendar | Search | Today's Posts | Mark Forums Read |
|
#1
| |||
| |||
| 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 |
|
#2
| |||
| |||
| 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. |
|
#3
| |||
| |||
| > 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; |
|
#4
| |||
| |||
| 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." |
|
#5
| |||
| |||
| 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 |
|
#6
| |||
| |||
| 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 |
|
#7
| |||
| |||
| 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 |
|
#8
| |||
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.