From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c71bf3c5adc59b21 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news2.google.com!news.glorb.com!feeder.erje.net!news2.arglkargh.de!nuzba.szn.dk!news.jacob-sparre.dk!pnx.dk!not-for-mail From: Jacob Sparre Andersen Newsgroups: comp.lang.ada Subject: Re: Opening a file vs. creating if not yet exists Date: 19 Aug 2008 16:23:49 +0200 Organization: Jacob's private Usenet server Sender: sparre@jspa-nykredit Message-ID: <87k5edjd6y.fsf@nbi.dk> References: NNTP-Posting-Host: 212.27.19.149.bredband.3.dk Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: jacob-sparre.dk 1219155830 24135 212.27.19.149 (19 Aug 2008 14:23:50 GMT) X-Complaints-To: news@jacob-sparre.dk NNTP-Posting-Date: Tue, 19 Aug 2008 14:23:50 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.4 Xref: g2news2.google.com comp.lang.ada:7378 Date: 2008-08-19T16:23:49+02:00 List-Id: 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."