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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.66.140.101 with SMTP id rf5mr27443067pab.14.1440490821363; Tue, 25 Aug 2015 01:20:21 -0700 (PDT) X-Received: by 10.182.213.164 with SMTP id nt4mr107352obc.39.1440490821326; Tue, 25 Aug 2015 01:20:21 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!u8no40589igq.0!news-out.google.com!nt1ni14385igb.0!nntp.google.com!u8no40580igq.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 25 Aug 2015 01:20:20 -0700 (PDT) In-Reply-To: <1rjn2z0rjbo2j$.1beneepusolhh.dlg@40tude.net> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=31.186.238.49; posting-account=bMuEOQoAAACUUr_ghL3RBIi5neBZ5w_S NNTP-Posting-Host: 31.186.238.49 References: <1rjn2z0rjbo2j$.1beneepusolhh.dlg@40tude.net> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Creating an empty file with Ada.Text_IO From: Maciej Sobczak Injection-Date: Tue, 25 Aug 2015 08:20:21 +0000 Content-Type: text/plain; charset=ISO-8859-1 Xref: news.eternal-september.org comp.lang.ada:27607 Date: 2015-08-25T01:20:20-07:00 List-Id: > > What are the recommended ways of: > > a) creating empty files > > b) writing a non-terminated line to (or generally at the end of) the file > > Stream I/O. Right. For those who might be looking for the same in the future: with Ada.Characters.Latin_1; with Ada.Streams.Stream_IO; procedure Test is procedure Write_String_To_File (File_Name : in String; S : in String) is A : Ada.Streams.Stream_Element_Array (1 .. S'Length); F : Ada.Streams.Stream_IO.File_Type; begin -- convert S to stream elements for I in S'Range loop A (Ada.Streams.Stream_Element_Offset (I)) := Ada.Streams.Stream_Element (Character'Pos (S (I))); end loop; -- create a stream file and write the content (which might be empty) Ada.Streams.Stream_IO.Create (F, Ada.Streams.Stream_IO.Out_File, File_Name); Ada.Streams.Stream_IO.Write (F, A); Ada.Streams.Stream_IO.Close (F); end Write_String_To_File; begin -- empty file: Write_String_To_File ("file1.txt", ""); -- file with one, non-terminated line: Write_String_To_File ("file2.txt", "abc"); -- file with "one line and a half": Write_String_To_File ("file3.txt", "abc" & Ada.Characters.Latin_1.LF & "xyz"); end Test; The above is, of course, platform-specific in the sense that some compatibility between characters and stream elements is assumed. This works everywhere. Having fun with platform-specific newline variants is left as an exercise to the reader. ;-) -- Maciej Sobczak * http://www.inspirel.com