From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Re: Creating an empty file with Ada.Text_IO
Date: Tue, 25 Aug 2015 01:20:20 -0700 (PDT)
Date: 2015-08-25T01:20:20-07:00 [thread overview]
Message-ID: <fa3d6ff4-d111-4564-a55f-33b2c0b076fa@googlegroups.com> (raw)
In-Reply-To: <1rjn2z0rjbo2j$.1beneepusolhh.dlg@40tude.net>
> > 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
next prev parent reply other threads:[~2015-08-25 8:20 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-08-24 14:16 Creating an empty file with Ada.Text_IO Maciej Sobczak
2015-08-24 16:15 ` Dmitry A. Kazakov
2015-08-25 8:20 ` Maciej Sobczak [this message]
2015-08-25 15:26 ` Maciej Sobczak
2015-08-25 16:18 ` J-P. Rosen
2015-08-25 16:45 ` G.B.
2015-08-24 18:51 ` AdaMagica
2015-08-24 22:22 ` Randy Brukardt
2015-08-25 0:06 ` Dennis Lee Bieber
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox