comp.lang.ada
 help / color / mirror / Atom feed
From: Dennis Lee Bieber <wlfraed@ix.netcom.com>
Subject: Re: How To Write A Record To File ?
Date: Fri, 08 Dec 2017 12:35:36 -0500
Date: 2017-12-08T12:35:36-05:00	[thread overview]
Message-ID: <omfl2d5rcfpdc0mv0ihkj076dkikvnt98g@4ax.com> (raw)
In-Reply-To: b8255ab8-8f54-48c1-9a43-db3e5eeb49c8@googlegroups.com

On Fri, 8 Dec 2017 08:04:31 -0800 (PST), patrick@spellingbeewinnars.org
declaimed the following:

>Hi Everyone
>
>Silly question here.
>
>I snagged this from an old book:
>
>
>       with sequential_io ;
>       procedure compute_total_population is
>        package si is        new sequential_io(integer);

	<SNIP>

	Using basic "integer" could be a problem, as it is dependent upon what
ever the compiler implementation defines to be base integer.

>sequential_io is a generic package but I can't get it to work with a type that is a record I created.
>

	HOW did you create your input file? What does it look like if
(hex)dumped.

	Sequential I/O does not do conversion between human-readable and
internal representations; the input file must be a sequence of binary
integers (all the same length -- implementation dependent), no line
formatting characters (so not something you can create with a text editor).

	If you created your file in an editor, with human-readable values, you
most likely need ada.text_io.integer_io

-=-=-=-=-	generate binary integer data file using Python
>>> import struct
>>> import random
>>> fout = open("d:\myTest.dat", "wb")
>>> for x in range(5):
... 	r = random.randint(0, 999999)
... 	print r
... 	b = struct.pack("i", r)
... 	fout.write(b)
... 	
476770
353201
930701
402627
273463
>>> fout.close()
>>> 

-=-=-=-=-	(using the Win10 Ubuntu BASH shell)

root@ElusiveUnicorn:~# od -t x4 /mnt/d/myTest.dat
0000000 00074662 000563b1 000e338d 000624c3
0000020 00042c37
0000024
root@ElusiveUnicorn:~#
root@ElusiveUnicorn:~# od -i /mnt/d/myTest.dat
0000000      476770      353201      930701      402627
0000020      273463
0000024
root@ElusiveUnicorn:~#

	I'd started with hexdump but couldn't figure out how to do 4-byte
integers; fortunately man suggested od, and those options were simpler for
this.

-=-=-=-=-	modified Ada source file
with Sequential_IO;
with Ada.Text_Io;
use Ada.Text_Io;

procedure seqio is
   package si is new Sequential_IO (Integer);
   data_file   : si.File_Type;
   value       : Integer;
   total       : Integer := 0;

begin

   si.Open (data_file, si.In_File, "d:\myTest.dat");
   while not si.End_Of_File (data_file) loop
      Si.Read (Data_File, Value);
      
      Put_line (Integer'Image (Value));
      
      total := total + value;
   end loop;

   si.Close (data_file);

   Put_Line ("The total is: " & Integer'Image (Total));
   
end seqio;

-=-=-=-=-	compile/link/run
C:\Users\Wulfraed\Documents\Ada Progs>gnatmake seqio.adb
gcc -c seqio.adb
gnatbind -x seqio.ali
gnatlink seqio.ali

C:\Users\Wulfraed\Documents\Ada Progs>seqio
 476770
 353201
 930701
 402627
 273463
The total is:  2436762

C:\Users\Wulfraed\Documents\Ada Progs>


	Works well here. (I really need to figure out how to set up a .gpr file
for scratch building test programs <G> )
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

  parent reply	other threads:[~2017-12-08 17:35 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-08 16:04 How To Write A Record To File ? patrick
2017-12-08 16:25 ` AdaMagica
2017-12-08 16:40   ` patrick
2017-12-08 16:41   ` patrick
2017-12-08 16:57     ` Anh Vo
2017-12-08 17:11       ` Anh Vo
2017-12-08 17:29         ` patrick
2017-12-08 16:43 ` Dmitry A. Kazakov
2017-12-08 16:53   ` patrick
2017-12-08 17:35 ` Dennis Lee Bieber [this message]
2017-12-08 18:01   ` Simon Wright
2017-12-08 23:06     ` patrick
2017-12-08 23:06       ` Victor Porton
2017-12-08 23:28       ` Simon Wright
2017-12-08 23:31         ` patrick
2017-12-09  8:03           ` Usenet (Was: How To Write A Record To File ?) Jacob Sparre Andersen
2017-12-09  9:08             ` Usenet Simon Wright
2017-12-09 14:38               ` Usenet Björn Lundin
2017-12-09 11:34           ` How To Write A Record To File ? Spiros Bousbouras
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox