comp.lang.ada
 help / color / mirror / Atom feed
* Extracting Fields in Files
@ 2000-01-12  0:00 jmoor
  0 siblings, 0 replies; 9+ messages in thread
From: jmoor @ 2000-01-12  0:00 UTC (permalink / raw)


I am reading a file containing variable length records, the maximum
size being 250 bytes and the minimum size 5 bytes.

I read each record by doing the following:

While not end_of_file(tip0125) loop
  Get_line (tip0125,rec01,rec01_length);
  .
  .

                METHOD 1
I break the individual fields up by doing the following:

Command_code := rec01(073..077);
Date_entered := rec01(001..008);
.
.
.

                METHOD 2
After writing the program, I bought an Ada book (Bad timing. I should
have bought the book before writing the program) that said that the
individual fields should be broken up by using the "text_io.get" on
each field.


Is it inherently wrong to use METHOD 1 to break up individual fields
instead of METHOD 2. I am experiencing problems with ignoring some
records, but I would doubt that METHOD 1 is the culprit.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Extracting Fields in Files
  2000-01-13  0:00 Christoph Grein
@ 2000-01-13  0:00 ` jmoor
  2000-01-13  0:00   ` David C. Hoos, Sr.
  0 siblings, 1 reply; 9+ messages in thread
From: jmoor @ 2000-01-13  0:00 UTC (permalink / raw)


Would you believe that I am reading a variable length record created on
a Unisys machine whose records are in the following sequence:

record    size
  #       (bytes)

1          16
2         212
3          16
4         212
5          16
6         212
7          16
8         212
9          16

I open the file with the following statement;
  text_io_open ( file => tip0125,
       mode => text_i.in_file,
       name => "tip0125");

Every time I read the file with the following statement;
  get_line (file => tip0125,
      item => rec01,
      last => rec01_length,)

After each 212 byte record is read, the get_line goes through a cycle
where it believes a zero byte record has been read.  I can easily get
around the problem by testing "rec01_length" for zero bytes, but this
is evidence that I might not understand the nature of the "get_line"
command when reading a variable length file.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Extracting Fields in Files
@ 2000-01-13  0:00 Christoph Grein
  2000-01-13  0:00 ` jmoor
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Grein @ 2000-01-13  0:00 UTC (permalink / raw)
  To: comp.lang.ada

There's nothing wrong with method 1.

However you could improve by introducing subtypes:

subtype Data_Range    is Positive range  1 ..  8;
subtype Command_Range is Positive range 73 .. 77;

Command_code := rec01 (Command_Range);
Date_entered := rec01 (Data_Range);









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

* Re: Extracting Fields in Files
  2000-01-13  0:00 ` jmoor
@ 2000-01-13  0:00   ` David C. Hoos, Sr.
  2000-01-14  0:00     ` jmoor
  0 siblings, 1 reply; 9+ messages in thread
From: David C. Hoos, Sr. @ 2000-01-13  0:00 UTC (permalink / raw)



<jmoor@my-deja.com> wrote in message news:85l0iu$6uj$1@nnrp1.deja.com...
> Would you believe that I am reading a variable length record created on
> a Unisys machine whose records are in the following sequence:
>
> record    size
>   #       (bytes)
>
> 1          16
> 2         212
> 3          16
> 4         212
> 5          16
> 6         212
> 7          16
> 8         212
> 9          16
>
> I open the file with the following statement;
>   text_io_open ( file => tip0125,
>        mode => text_i.in_file,
>        name => "tip0125");
>
> Every time I read the file with the following statement;
>   get_line (file => tip0125,
>       item => rec01,
>       last => rec01_length,)
>
> After each 212 byte record is read, the get_line goes through a cycle
> where it believes a zero byte record has been read.  I can easily get
> around the problem by testing "rec01_length" for zero bytes, but this
> is evidence that I might not understand the nature of the "get_line"
> command when reading a variable length file.
Well.... Ada doesn't really speak of file "records," as such.

Ada.Text_IO.Get_Line reads based on whatever the line terminator is
for the OS in question.  Reading a file on an OS with a different line
termination convention may produce unexpected results.

You may find it more useful to use Ada.Streams.Stream_IO, because it
allows you to read the file as a stream of bytes -- in which case you can
do your own thing to recognize line terminators.

From your earlier post it appeared that your code also depended on
the fields beginning and ending in specific columns.  You may find
the procedures like Ada.Strings.Fixed.Find_Token helpful in breaking
a line up into tokens if the fields are always delimited by characters
that never appear within any field.








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

* Re: Extracting Fields in Files
  2000-01-14  0:00 Christoph Grein
@ 2000-01-14  0:00 ` jmoor
  0 siblings, 0 replies; 9+ messages in thread
From: jmoor @ 2000-01-14  0:00 UTC (permalink / raw)


In article <200001140610.HAA09245@bulgaria.otn.eurocopter.de>,
  comp.lang.ada@ada.eu.org wrote:
> that reading exactly 212 characters will leave the LF (the end of
line on
> Unix, i.e. end of record, as you call it) unread. So your next
get_line simply
> reads the LF and returns an empty line.
>
> As a simple work-around declare
>
>   rec01: String (1..213);
>
> (one longer than the maximum expected length) and a get_line will
always
> swallow the LF.
>
>

I increased the length of the rec01 string to 213 as you suggested.  I
printed a report showing record number, type, and length returned from
get_line and that program modification did stop get_line from returning
an empty line.

The report also showed that record 193 was an inappropriate length for
the type (in Cobolese "wrong length record").  The program prematurely
goes to end of job after reading record 193.  Wouldn't you think that
an exception would have been raised?  Even if I wrote a routine that
trapped "wrong length records" how could I bypass the "record"?  Did
the program stop after reading the record because it truly sensed an
end of file condition or because it sensed an exception?

Learning a new language is not easy (esp. for old dogs).


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Extracting Fields in Files
  2000-01-13  0:00   ` David C. Hoos, Sr.
@ 2000-01-14  0:00     ` jmoor
  2000-01-14  0:00       ` Ted Dennison
  2000-01-15  0:00       ` Gautier
  0 siblings, 2 replies; 9+ messages in thread
From: jmoor @ 2000-01-14  0:00 UTC (permalink / raw)


In article <85l7t4$18c$1@hobbes2.crc.com>,
  "David C. Hoos, Sr." <david.c.hoos.sr@ada95.com> wrote:
> You may find it more useful to use Ada.Streams.Stream_IO, because it
> allows you to read the file as a stream of bytes -- in which case you
can
>

I am not able to read all my downloaded records in Perl -- end of file
is sensed at record 193,  just as in Ada.

I am not able to read all my downloaded records in C when I open the
file read (ie "r"), but when I open the file read-binary (ie "r+b")all
records are read.

I have not found "stream io" explained in any book.  The RM seems
unintelligible (I don't speak Noir notation). I can't find an example
of stream io.  Isn't there an easy way of reading binary in Ada
(eg. "r+b").  I will surely loose what little sanity I have, if I have
to code in C.


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Extracting Fields in Files
  2000-01-14  0:00     ` jmoor
@ 2000-01-14  0:00       ` Ted Dennison
  2000-01-15  0:00       ` Gautier
  1 sibling, 0 replies; 9+ messages in thread
From: Ted Dennison @ 2000-01-14  0:00 UTC (permalink / raw)


In article <85nsv7$b6b$1@nnrp1.deja.com>,
  jmoor@my-deja.com wrote:
> In article <85l7t4$18c$1@hobbes2.crc.com>,
> I have not found "stream io" explained in any book.  The RM seems
> unintelligible (I don't speak Noir notation). I can't find an example
> of stream io.  Isn't there an easy way of reading binary in Ada
> (eg. "r+b").  I will surely loose what little sanity I have, if I have
> to code in C.

Well, you could use Sequential_IO. But I'd think streams would be the
easiest approach. Cohen's Ada as a Second Language devotes a couple of
pages to Stream_IO.

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html


Sent via Deja.com http://www.deja.com/
Before you buy.




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

* Re: Extracting Fields in Files
@ 2000-01-14  0:00 Christoph Grein
  2000-01-14  0:00 ` jmoor
  0 siblings, 1 reply; 9+ messages in thread
From: Christoph Grein @ 2000-01-14  0:00 UTC (permalink / raw)
  To: comp.lang.ada

When you do

 get_line (file => tip0125,
           item => rec01,
           last => rec01_length);

I presume you have defined rec01 (knowing that the maximum record
length is 212) as

  rec01: String (1..212);

Now read very carefully the corresponding chapter in the RM and you'll find
that reading exactly 212 characters will leave the LF (the end of line on
Unix, i.e. end of record, as you call it) unread. So your next get_line simply
reads the LF and returns an empty line.

As a simple work-around declare

  rec01: String (1..213);

(one longer than the maximum expected length) and a get_line will always
swallow the LF.









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

* Re: Extracting Fields in Files
  2000-01-14  0:00     ` jmoor
  2000-01-14  0:00       ` Ted Dennison
@ 2000-01-15  0:00       ` Gautier
  1 sibling, 0 replies; 9+ messages in thread
From: Gautier @ 2000-01-15  0:00 UTC (permalink / raw)


> I have not found "stream io" explained in any book.  The RM seems
> unintelligible (I don't speak Noir notation). I can't find an example
> of stream io.  Isn't there an easy way of reading binary in Ada
> (eg. "r+b").  I will surely loose what little sanity I have, if I have
> to code in C.

Ok ok don't panic. Here are examples (in SVGA.IO)
  http://members.xoom.com/gdemont/logiciel/svga02x4.zip
with i/o on images. [I waited ~1 year, with questions in c.l.a.,
to at last obtain a simple example!]

-- 
Gautier

_____\\________________\_______\_________
http://members.xoom.com/gdemont/gsoft.htm




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

end of thread, other threads:[~2000-01-15  0:00 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-01-12  0:00 Extracting Fields in Files jmoor
  -- strict thread matches above, loose matches on Subject: below --
2000-01-13  0:00 Christoph Grein
2000-01-13  0:00 ` jmoor
2000-01-13  0:00   ` David C. Hoos, Sr.
2000-01-14  0:00     ` jmoor
2000-01-14  0:00       ` Ted Dennison
2000-01-15  0:00       ` Gautier
2000-01-14  0:00 Christoph Grein
2000-01-14  0:00 ` jmoor

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