comp.lang.ada
 help / color / mirror / Atom feed
* How To Write A Record To File ?
@ 2017-12-08 16:04 patrick
  2017-12-08 16:25 ` AdaMagica
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: patrick @ 2017-12-08 16:04 UTC (permalink / raw)


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);
        data_file          : si.file_type ;
        result_file        : si.file_type ;
        value              : integer ;
        total              : integer := 0 ;

       begin 

        si.open(data_file, si.in_file, "census74") ;
        while not si.end_of_file(data_file)
         loop
          si.read(data_file, value) ;
          total := total + value ;
         end loop ;

        si.close(data_file) ;

        si.create(result_file, name => "total74") ;
        si.write(result_file, total);
        si.close(result_file) ;

       end compute_total_population ;

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

Is there a way to do this, perhaps another library?

Thanks for reading-Patrick


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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:43 ` Dmitry A. Kazakov
  2017-12-08 17:35 ` Dennis Lee Bieber
  2 siblings, 2 replies; 19+ messages in thread
From: AdaMagica @ 2017-12-08 16:25 UTC (permalink / raw)


Am Freitag, 8. Dezember 2017 17:04:33 UTC+1 schrieb 
> sequential_io is a generic package but I can't get it to work with a type that is a record I created.
> 
> Is there a way to do this, perhaps another library?

What's your problem? It doesn't work isn't a problem description.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:25 ` AdaMagica
@ 2017-12-08 16:40   ` patrick
  2017-12-08 16:41   ` patrick
  1 sibling, 0 replies; 19+ messages in thread
From: patrick @ 2017-12-08 16:40 UTC (permalink / raw)


Hi AdaMagica


Please see this failing program to see what I mean.

Thanks




       with sequential_io ;
       procedure compute_total_population is
        type foo_type is
        record
          moo              : integer ;
          boo              : integer ;
        end record ;
 
        foo                : foo_type ;
       -- will fail here with error:
       -- expect valid subtype mark to instantiate "Element_Type"
        package si is        new sequential_io(foo);

        data_file          : si.file_type ;
        result_file        : si.file_type ;
        value              : integer ;
        total              : integer := 0 ;




       begin 

        si.open(data_file, si.in_file, "census74") ;
        while not si.end_of_file(data_file)
         loop
          si.read(data_file, value) ;
          total := total + value ;
         end loop ;

        si.close(data_file) ;

        si.create(result_file, name => "total74") ;
        si.write(result_file, total);
        si.close(result_file) ;

       end compute_total_population ;

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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
  1 sibling, 1 reply; 19+ messages in thread
From: patrick @ 2017-12-08 16:41 UTC (permalink / raw)


Hi Adamagica

Please see this failing program:

-Patrick

       with sequential_io ;
       procedure compute_total_population is
        type foo_type is
        record
          moo              : integer ;
          boo              : integer ;
        end record ;
 
        foo                : foo_type ;
       -- will fail here with error:
       -- expect valid subtype mark to instantiate "Element_Type"
        package si is        new sequential_io(foo);

        data_file          : si.file_type ;
        result_file        : si.file_type ;
        value              : integer ;
        total              : integer := 0 ;




       begin 

        si.open(data_file, si.in_file, "census74") ;
        while not si.end_of_file(data_file)
         loop
          si.read(data_file, value) ;
          total := total + value ;
         end loop ;

        si.close(data_file) ;

        si.create(result_file, name => "total74") ;
        si.write(result_file, total);
        si.close(result_file) ;

       end compute_total_population ;

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:04 How To Write A Record To File ? patrick
  2017-12-08 16:25 ` AdaMagica
@ 2017-12-08 16:43 ` Dmitry A. Kazakov
  2017-12-08 16:53   ` patrick
  2017-12-08 17:35 ` Dennis Lee Bieber
  2 siblings, 1 reply; 19+ messages in thread
From: Dmitry A. Kazakov @ 2017-12-08 16:43 UTC (permalink / raw)


On 2017-12-08 17:04, patrick@spellingbeewinnars.org wrote:

> Is there a way to do this, perhaps another library?

Yes.

1. Don't use Sequential_IO, use Stream_IO.

2. Don't use end of file test, it is not Ada's way, and no way for many 
types of files. End_Error is the safest, cleanest and most efficient 
method to handle file end.

3. For reading legacy files implement stream attributes of components to 
be conform with the format of the file.

(In general, avoid default implementation of stream attributes when 
dealing with external files or protocols)

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:43 ` Dmitry A. Kazakov
@ 2017-12-08 16:53   ` patrick
  0 siblings, 0 replies; 19+ messages in thread
From: patrick @ 2017-12-08 16:53 UTC (permalink / raw)


Thanks Dmitry !

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:41   ` patrick
@ 2017-12-08 16:57     ` Anh Vo
  2017-12-08 17:11       ` Anh Vo
  0 siblings, 1 reply; 19+ messages in thread
From: Anh Vo @ 2017-12-08 16:57 UTC (permalink / raw)


On Friday, December 8, 2017 at 8:41:57 AM UTC-8, pat...@spellingbeewinnars.org wrote:
> Hi Adamagica
> 
> Please see this failing program:
> 
> -Patrick
> 
>        with sequential_io ;
>        procedure compute_total_population is
>         type foo_type is
>         record
>           moo              : integer ;
>           boo              : integer ;
>         end record ;
>  
>         foo                : foo_type ;
>        -- will fail here with error:
>        -- expect valid subtype mark to instantiate "Element_Type"
>         package si is        new sequential_io(foo);

Your basic is error to instantiate with an object of record instead of record type. Here is the correct instantiation.

   package SI is new Ada.Sequential_IO (Foo_Type);

Anh Vo

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:57     ` Anh Vo
@ 2017-12-08 17:11       ` Anh Vo
  2017-12-08 17:29         ` patrick
  0 siblings, 1 reply; 19+ messages in thread
From: Anh Vo @ 2017-12-08 17:11 UTC (permalink / raw)


On Friday, December 8, 2017 at 8:57:24 AM UTC-8, Anh Vo wrote:
> On Friday, December 8, 2017 at 8:41:57 AM UTC-8, pat...@spellingbeewinnars.org wrote:
> > Hi Adamagica
> > 
> > Please see this failing program:
> > 
> > -Patrick
> > 
> >        with sequential_io ;
> >        procedure compute_total_population is
> >         type foo_type is
> >         record
> >           moo              : integer ;
> >           boo              : integer ;
> >         end record ;
> >  
> >         foo                : foo_type ;
> >        -- will fail here with error:
> >        -- expect valid subtype mark to instantiate "Element_Type"
> >         package si is        new sequential_io(foo);
> 
> Your basic is error to instantiate with an object of record instead of record type. Here is the correct instantiation.
> 
>    package SI is new Ada.Sequential_IO (Foo_Type);
> 
> Anh Vo

I meant "your basic error is to ... "

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 17:11       ` Anh Vo
@ 2017-12-08 17:29         ` patrick
  0 siblings, 0 replies; 19+ messages in thread
From: patrick @ 2017-12-08 17:29 UTC (permalink / raw)


Thanks! Anh Vo

It's so obvious now

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 16:04 How To Write A Record To File ? patrick
  2017-12-08 16:25 ` AdaMagica
  2017-12-08 16:43 ` Dmitry A. Kazakov
@ 2017-12-08 17:35 ` Dennis Lee Bieber
  2017-12-08 18:01   ` Simon Wright
  2 siblings, 1 reply; 19+ messages in thread
From: Dennis Lee Bieber @ 2017-12-08 17:35 UTC (permalink / raw)


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/

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 17:35 ` Dennis Lee Bieber
@ 2017-12-08 18:01   ` Simon Wright
  2017-12-08 23:06     ` patrick
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Wright @ 2017-12-08 18:01 UTC (permalink / raw)


Dennis Lee Bieber <wlfraed@ix.netcom.com> writes:

> 	Works well here. (I really need to figure out how to set up a
> .gpr file for scratch building test programs <G> )

So long as you don't have _any_ GPRs in your directory, you don't need
to provide your own, as there is one in $prefix/share/gpr/_default.gpr;
it just says

   standard project Default is
   end Default;

Demo: only one file here:

   lockheed:dennis simon$ ls
   dennis.adb

just gprbuild:

   lockheed:dennis simon$ gprbuild
   using project file /opt/gcc-7.1.0/share/gpr/_default.gpr
   Compile
      [Ada]          dennis.adb

name the main program:

   lockheed:dennis simon$ gprbuild dennis.adb
   using project file /opt/gcc-7.1.0/share/gpr/_default.gpr
   Bind
      [gprbind]      dennis.bexch
      [Ada]          dennis.ali
   Link
      [link]         dennis.adb

run it:

   lockheed:dennis simon$ ./dennis 
   hello Dennis!


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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
  0 siblings, 2 replies; 19+ messages in thread
From: patrick @ 2017-12-08 23:06 UTC (permalink / raw)


Does anyone have a way to contact Simon, I think he might have posted to the wrong thread by accident. I don't know how to PM people

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  2017-12-08 23:06     ` patrick
@ 2017-12-08 23:06       ` Victor Porton
  2017-12-08 23:28       ` Simon Wright
  1 sibling, 0 replies; 19+ messages in thread
From: Victor Porton @ 2017-12-08 23:06 UTC (permalink / raw)


patrick@spellingbeewinnars.org wrote:

> Does anyone have a way to contact Simon, I think he might have posted to
> the wrong thread by accident. I don't know how to PM people

Patrick, by email.

-- 
Victor Porton - http://portonvictor.org

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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
  1 sibling, 1 reply; 19+ messages in thread
From: Simon Wright @ 2017-12-08 23:28 UTC (permalink / raw)


patrick@spellingbeewinnars.org writes:

> Does anyone have a way to contact Simon, I think he might have posted
> to the wrong thread by accident. I don't know how to PM people

Well, you might as well have posted your actual comment here!

The part of Dennis's post that I responded to is on the very last line
of his post, just above the signature.

Oh! I see that you are using Google Groups, and when I go to check ..

(a) Dennis's post isn't there

(b) Victor suggested e-mail, but I don't see how you can do that (the
option to "reply privately" isn't shown, perhaps because I'm not posting
from a gmail address, though it seems to be OK for others. A restrictive
practice on Google's part?)


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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 11:34           ` How To Write A Record To File ? Spiros Bousbouras
  0 siblings, 2 replies; 19+ messages in thread
From: patrick @ 2017-12-08 23:31 UTC (permalink / raw)


Hi Simon

I actually hate using Google Groups, could you tell me where you access this list from ?

-Patrick

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Usenet (Was: How To Write A Record To File ?)
  2017-12-08 23:31         ` patrick
@ 2017-12-09  8:03           ` Jacob Sparre Andersen
  2017-12-09  9:08             ` Usenet Simon Wright
  2017-12-09 11:34           ` How To Write A Record To File ? Spiros Bousbouras
  1 sibling, 1 reply; 19+ messages in thread
From: Jacob Sparre Andersen @ 2017-12-09  8:03 UTC (permalink / raw)


patrick@spellingbeewinnars.org writes:

> I actually hate using Google Groups, could you tell me where you
> access this list from?

It is a Usenet newsgroup, and you can access it through most Usenet
providers.

One free option is dotsrc.org: https://dotsrc.org/usenet/

Greetings,

Jacob
-- 
"It ain't rocket science!" - Well, some of it is!


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Usenet
  2017-12-09  8:03           ` Usenet (Was: How To Write A Record To File ?) Jacob Sparre Andersen
@ 2017-12-09  9:08             ` Simon Wright
  2017-12-09 14:38               ` Usenet Björn Lundin
  0 siblings, 1 reply; 19+ messages in thread
From: Simon Wright @ 2017-12-09  9:08 UTC (permalink / raw)


Jacob Sparre Andersen <jacob@jacob-sparre.dk> writes:

> patrick@spellingbeewinnars.org writes:
>
>> I actually hate using Google Groups, could you tell me where you
>> access this list from?
>
> It is a Usenet newsgroup, and you can access it through most Usenet
> providers.
>
> One free option is dotsrc.org: https://dotsrc.org/usenet/

Another: http://www.eternal-september.org

The other thing Patrick needs is a news reader. I use Gnus in Emacs; not
everyone's choice.

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: How To Write A Record To File ?
  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 11:34           ` Spiros Bousbouras
  1 sibling, 0 replies; 19+ messages in thread
From: Spiros Bousbouras @ 2017-12-09 11:34 UTC (permalink / raw)


On Fri, 8 Dec 2017 15:31:42 -0800 (PST)
patrick@spellingbeewinnars.org wrote:
> Hi Simon
> 
> I actually hate using Google Groups, could you tell me where you access this list from ?

To add some more details to what others have said , you download a
newsreader for your operating system. The documentation will mention
somewhere how to configure it to use some newsserver. So you do that and you
should be set to go. A free newsserver is  news.aioe.org .Your ISP may also
provide a newsserver although they might not advertise it on their brochures.
Google for
    usenet <your ISP>
and see what you find. For HTML access to usenet there is also  webuse.net .

-- 
vlaho.ninja/prog


^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: Usenet
  2017-12-09  9:08             ` Usenet Simon Wright
@ 2017-12-09 14:38               ` Björn Lundin
  0 siblings, 0 replies; 19+ messages in thread
From: Björn Lundin @ 2017-12-09 14:38 UTC (permalink / raw)


On 2017-12-09 10:08, Simon Wright wrote:
> The other thing Patrick needs is a news reader. I use Gnus in Emacs; not
> everyone's choice.

I use Thunderbird

-- 
--
Björn


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2017-12-09 14:38 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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