comp.lang.ada
 help / color / mirror / Atom feed
* Re: Binary Data File - PLEASE HELP
       [not found] <n5su6.24687$CL6.767652@telenews.teleline.es>
@ 2001-03-26 14:08 ` Sergio
  2001-03-26 14:49   ` chris.danx
                     ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Sergio @ 2001-03-26 14:08 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 588 bytes --]

Ada simply cannot do that, sorry.
Ada sucks.

"Sergio" <alguien@microsoft.com> escribi� en el mensaje
news:n5su6.24687$CL6.767652@telenews.teleline.es...
> How could I do this in Ada? Could I do it at a reasonable speed? I don't
> know how, please help me. Thanks
>
> void WorkFile(char *fileName)
> {
>     char Buffer[4096];
>     FILE *handle;
>
>     handle = fopen(fileName, "r");
>     while (!feof(handle))
>     {
>         fread(handle, 4096, 1, buffer); /* Read data from file to buffer
*/
>         Work(Buffer);   /* ... do some work with such data */
>     }
> }
>
>
>





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
@ 2001-03-26 14:49   ` chris.danx
  2001-03-26 14:56     ` Marin David Condic
  2001-03-26 14:55   ` Marin David Condic
                     ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: chris.danx @ 2001-03-26 14:49 UTC (permalink / raw)


Try using Direct_IO instantiated for type Char, or a mod 'byte' type

i.e.    type byte is mod 256;

then write a procedure

-- read a sequence of bytes upto numb, from file;
-- if reaches EOF first raise exception EOF_Error;
--
procedure fread (file         : in File_Type;
                           bytes     : out byte_array_type;
                           numb    : in Natural);

the procedure simply uses a while loop or a for loop to read from the file
numb times.

-- though i'm not sure how to get an unconstrained array to work for this;

type byte_array_type is array(natural range <>) of byte;

would be ok, but it might not work with the above procedure.  Someone else
will have to help you with that.  I'd be interested to get any number of
bytes out in an unconstrained type.  Anyone know how?

Chris Campbell





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
  2001-03-26 14:49   ` chris.danx
@ 2001-03-26 14:55   ` Marin David Condic
  2001-03-26 19:51     ` Ted Dennison
  2001-03-26 14:56   ` Ted Dennison
                     ` (2 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Marin David Condic @ 2001-03-26 14:55 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1291 bytes --]

Nice bit of flame-bait.

If you're really serious and this is not simply a troll to get someone to do
a homework assignment......

You might try looking at the packages: Ada.Streams (ARM 13.13.1),
Ada.Streams.Stream_IO (ARM A.12.1), Ada.Text_IO.Text_Streams (ARM A.12.2)

Somewhere in those packages lies the answer to all your problems.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/



"Sergio" <alguien@microsoft.com> wrote in message
news:J3Iv6.56705$CL6.1997020@telenews.teleline.es...
> Ada simply cannot do that, sorry.
> Ada sucks.
>
> "Sergio" <alguien@microsoft.com> escribi� en el mensaje
> news:n5su6.24687$CL6.767652@telenews.teleline.es...
> > How could I do this in Ada? Could I do it at a reasonable speed? I don't
> > know how, please help me. Thanks
> >
> > void WorkFile(char *fileName)
> > {
> >     char Buffer[4096];
> >     FILE *handle;
> >
> >     handle = fopen(fileName, "r");
> >     while (!feof(handle))
> >     {
> >         fread(handle, 4096, 1, buffer); /* Read data from file to buffer
> */
> >         Work(Buffer);   /* ... do some work with such data */
> >     }
> > }
> >
> >
> >
>
>





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
  2001-03-26 14:49   ` chris.danx
  2001-03-26 14:55   ` Marin David Condic
@ 2001-03-26 14:56   ` Ted Dennison
  2001-03-26 15:09     ` Marin David Condic
  2001-03-26 15:21   ` Martin Dowie
  2001-04-05  5:08   ` David Thompson
  4 siblings, 1 reply; 13+ messages in thread
From: Ted Dennison @ 2001-03-26 14:56 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1475 bytes --]

In article <J3Iv6.56705$CL6.1997020@telenews.teleline.es>, Sergio says...
>
>Ada simply cannot do that, sorry.
BZZZT! Wrong. 
There are no less than 3 ways to do this (perhaps more).

You can:
1) Use Ada.Streams.Stream_IO and Ada.Streams.Read (read must be called directly
with the entire buffer).
2) Use Ada.Sequential_IO (it'll have to be instantiated with the whole buffer as
its type).
3) Pragma interface to C and use the same calls you did below (it's a dumb way
to do it rather than the other 2 options, but it works).

>"Sergio" <alguien@microsoft.com> escribi� en el mensaje
>news:n5su6.24687$CL6.767652@telenews.teleline.es...
>> How could I do this in Ada? Could I do it at a reasonable speed? I don't
>> know how, please help me. Thanks
>>
>> void WorkFile(char *fileName)
>> {
>>     char Buffer[4096];
>>     FILE *handle;
>>
>>     handle = fopen(fileName, "r");
>>     while (!feof(handle))
>>     {
>>         fread(handle, 4096, 1, buffer); /* Read data from file to buffer
>*/
>>         Work(Buffer);   /* ... do some work with such data */
>>     }
>> }

If you'd like to see some code I have laying around that does almost exactly
this, just say the word and I'll post it.

About the only thing you can do with C that you can't do with Ada is have a
yearly obfuscated code contest with thousands of worthy submissions. :-)

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:49   ` chris.danx
@ 2001-03-26 14:56     ` Marin David Condic
  0 siblings, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2001-03-26 14:56 UTC (permalink / raw)


Not really the best way to go. Look at Stream_IO as I mentioned in another
post...

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/

"chris.danx" <chris.danx@ntlworld.com> wrote in message
news:%FIv6.31761$Q4.5539631@news2-win.server.ntlworld.com...
> Try using Direct_IO instantiated for type Char, or a mod 'byte' type
>
> i.e.    type byte is mod 256;
>
> then write a procedure
>
> -- read a sequence of bytes upto numb, from file;
> -- if reaches EOF first raise exception EOF_Error;
> --
> procedure fread (file         : in File_Type;
>                            bytes     : out byte_array_type;
>                            numb    : in Natural);
>
> the procedure simply uses a while loop or a for loop to read from the file
> numb times.
>
> -- though i'm not sure how to get an unconstrained array to work for this;
>
> type byte_array_type is array(natural range <>) of byte;
>
> would be ok, but it might not work with the above procedure.  Someone else
> will have to help you with that.  I'd be interested to get any number of
> bytes out in an unconstrained type.  Anyone know how?
>
> Chris Campbell
>
>





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:56   ` Ted Dennison
@ 2001-03-26 15:09     ` Marin David Condic
  0 siblings, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2001-03-26 15:09 UTC (permalink / raw)


Please, Ted! Let's not throw down that gauntlet. :-) There isn't a language
anywhere in which it is the least bit hard to write bad code.

Maybe we should start a thread here: "Betcha Can't Figure Out What This One
Does!!!" Write a subprogram in 100 semicolons or less that is totally
compilable, produces correct results, but its up to the readers to figure
out what it does. The winner earns the title "Ada Geek Of The Year."

The only problem is that most of us here just aren't interested in that sort
of problem - we'd rather figure out how to make code as clear as an
unmuddied lake.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/


"Ted Dennison" <dennison@telepath.com> wrote in message
news:mMIv6.2825$fy.4168@www.newsranger.com...
> About the only thing you can do with C that you can't do with Ada is have
a
> yearly obfuscated code contest with thousands of worthy submissions. :-)
>






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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
                     ` (2 preceding siblings ...)
  2001-03-26 14:56   ` Ted Dennison
@ 2001-03-26 15:21   ` Martin Dowie
  2001-04-05  5:08   ` David Thompson
  4 siblings, 0 replies; 13+ messages in thread
From: Martin Dowie @ 2001-03-26 15:21 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2574 bytes --]

er, except it can...

I've even thrown in some exception handling

First the test program and you can cut-and-paste the
Ada code you need to mimic your original C code
Secondly the output of running this program.

Should be portable, but for reference, I used a
700MHz Intel Celeron
WinNT 4 (Build 1381)
GNAT 3.13p

---------------------------------------------------------
test program:
with Ada.Sequential_IO;
with Ada.Text_IO; use Ada.Text_IO;

procedure Tester is

  subtype Buffer is String (1 .. 4_096);

  My_Buffer : Buffer;

  package IO is new Ada.Sequential_IO (Buffer);

  My_File : IO.File_Type;

begin

  Put_Line ("File 1");

  -- Have tested this and it works just fine
  -- Note the big difference is in the handling
  -- of elements that are not the same size as
  -- the buffer. In Ada, you get an exception - coz
  -- your input data is valid, right? In C, you
  -- get a return value that doesn't equal the
  -- value of 'size'.
  --
  -- This file is of the correct size
  --
  IO.Open (My_File, IO.In_File, "test.txt");

  while not IO.End_Of_File (My_File) loop
    begin
      IO.Read (My_File, My_Buffer);
    exception
      when others =>
        Put_Line ("oops");
        exit;
    end;
  end loop;

  IO.Close (My_File);


  Put_Line ("File 2");

  -- This file is one byte too big
  --
  IO.Open (My_File, IO.In_File, "test2.txt");

  while not IO.End_Of_File (My_File) loop
    begin
      IO.Read (My_File, My_Buffer);
    exception
      when others =>
        Put_Line ("oops");
        exit;
    end;
  end loop;

  IO.Close (My_File);

  Put_Line ("Finished");

exception
  when others =>
    Put_Line ("Exception");
    if IO.Is_Open (My_File) then
      IO.Close (My_File);
    end if;
end Tester;

---------------------------------------------------------
result:
File 1
File 2
oops
Finished

Sergio <alguien@microsoft.com> wrote in message
news:J3Iv6.56705$CL6.1997020@telenews.teleline.es...
> Ada simply cannot do that, sorry.
> Ada sucks.
>
> "Sergio" <alguien@microsoft.com> escribi� en el mensaje
> news:n5su6.24687$CL6.767652@telenews.teleline.es...
> > How could I do this in Ada? Could I do it at a reasonable speed? I don't
> > know how, please help me. Thanks
> >
> > void WorkFile(char *fileName)
> > {
> >     char Buffer[4096];
> >     FILE *handle;
> >
> >     handle = fopen(fileName, "r");
> >     while (!feof(handle))
> >     {
> >         fread(handle, 4096, 1, buffer); /* Read data from file to buffer
> */
> >         Work(Buffer);   /* ... do some work with such data */
> >     }
> > }
> >
> >
> >
>
>





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:55   ` Marin David Condic
@ 2001-03-26 19:51     ` Ted Dennison
  2001-03-26 20:55       ` Marin David Condic
  2001-03-26 21:16       ` tmoran
  0 siblings, 2 replies; 13+ messages in thread
From: Ted Dennison @ 2001-03-26 19:51 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1541 bytes --]

In article <99nl9l$17s$1@nh.pace.co.uk>, Marin David Condic says...
>If you're really serious and this is not simply a troll to get someone to do
>a homework assignment......

I certianly hope not. It would be awfully advanced for CS1 or CS2 homework. Plus
generally no-one cares how fast their school projects run, as long as the right
answers come out within half an hour or so.

Still, you have to find it odd that the second message (the flame-bait) was
posted by the same person who posted the first one. Even more interesting is
that the first message never appeared on my newsserver. I just figured that
Sergio's first message never got out, but he thought it was being ignored. So to
ensure a good response, he posted a follow-up with the entire content quoted,
and nothing but flame-bait added.

But the first posting was still a reasonable question, which deserves a
resonable answer. Others out there are probably curious about the same thing. I
hope we can all strive to keep it as civil as Marin has, despite the flame-bait.


Perhaps it would be of some help to Sergio and others in the future if we can
get a good short example posted on AdaPower in the source code treasury.

>"Sergio" <alguien@microsoft.com> wrote in message
>news:J3Iv6.56705$CL6.1997020@telenews.teleline.es...
>> "Sergio" <alguien@microsoft.com> escribi� en el mensaje
>> news:n5su6.24687$CL6.767652@telenews.teleline.es...

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 19:51     ` Ted Dennison
@ 2001-03-26 20:55       ` Marin David Condic
  2001-03-26 21:16       ` tmoran
  1 sibling, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2001-03-26 20:55 UTC (permalink / raw)


It is, in a way, a reasonable question. What bothered me was that it was
entirely coded up in C, but no attempt to code anything similar in Ada. One
would expect to see some code with a question of the form "Why won't feature
X work?" Or a more general sort of question like "How do I read binary files
in Ada?" rather than an example in C and an apparent request for an Ada
translation... That was why I kind of thought it might be homework.

You're right about this being a rather common question. I think I have an
example of similar code on my web site, but I can't get there at the moment
to make sure. A small program to, perhaps, hex-dump an arbitrary file would
be a good example. Clearly, AdaPower would be a good place for it.

Thanks for the kudos on my civility. I've strived to be a civilian all my
life. :-)

MDC

--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/



"Ted Dennison" <dennison@telepath.com> wrote in message
news:k5Nv6.2984$fy.5082@www.newsranger.com...
>
> But the first posting was still a reasonable question, which deserves a
> resonable answer. Others out there are probably curious about the same
thing. I
> hope we can all strive to keep it as civil as Marin has, despite the
flame-bait.
>
>





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 19:51     ` Ted Dennison
  2001-03-26 20:55       ` Marin David Condic
@ 2001-03-26 21:16       ` tmoran
  2001-03-26 22:16         ` Marin David Condic
  2001-03-26 22:41         ` Ted Dennison
  1 sibling, 2 replies; 13+ messages in thread
From: tmoran @ 2001-03-26 21:16 UTC (permalink / raw)


>just figured that Sergio's first message never got out, but he thought it
>was being ignored.  So to ensure a good response, he posted a follow-up
  Interesting.  Your message references 3 previous messages.  This is the
first message on this subject I've seen, and Google's dejanews replacement
doesn't show any.  Has this thread been visible to other people?  (e-mail
tmoran@acm.org and I'll post a summary message)



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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 21:16       ` tmoran
@ 2001-03-26 22:16         ` Marin David Condic
  2001-03-26 22:41         ` Ted Dennison
  1 sibling, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2001-03-26 22:16 UTC (permalink / raw)


The thread is a few posts deep. I have been seeing spotty response as well,
but our company is currently experiencing some Internet problems & that's
what I've been blaming. Maybe it is more widespread? Can the Internet call
in sick? :-)

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/

<tmoran@acm.org> wrote in message
news:NkOv6.7162$ad1.775934@news1.frmt1.sfba.home.com...
> >just figured that Sergio's first message never got out, but he thought it
> >was being ignored.  So to ensure a good response, he posted a follow-up
>   Interesting.  Your message references 3 previous messages.  This is the
> first message on this subject I've seen, and Google's dejanews replacement
> doesn't show any.  Has this thread been visible to other people?  (e-mail
> tmoran@acm.org and I'll post a summary message)





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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 21:16       ` tmoran
  2001-03-26 22:16         ` Marin David Condic
@ 2001-03-26 22:41         ` Ted Dennison
  1 sibling, 0 replies; 13+ messages in thread
From: Ted Dennison @ 2001-03-26 22:41 UTC (permalink / raw)


In article <NkOv6.7162$ad1.775934@news1.frmt1.sfba.home.com>, tmoran@acm.org
says...
>
>>just figured that Sergio's first message never got out, but he thought it
>>was being ignored.  So to ensure a good response, he posted a follow-up
>  Interesting.  Your message references 3 previous messages.  This is the
>first message on this subject I've seen, and Google's dejanews replacement
>doesn't show any.  Has this thread been visible to other people?  (e-mail

I never saw the supposed first message from Sergio. At the time, I didn't think
much of it because Usenet messages quite often come in out of order. But it
still hasn't shown up here.

I haven't gone back to Google since the day Deja died. I'm using Newsranger now.
It has its quirks (just like Deja did), but its free and seems to be quite
servicable for the purpose.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: Binary Data File - PLEASE HELP
  2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
                     ` (3 preceding siblings ...)
  2001-03-26 15:21   ` Martin Dowie
@ 2001-04-05  5:08   ` David Thompson
  4 siblings, 0 replies; 13+ messages in thread
From: David Thompson @ 2001-04-05  5:08 UTC (permalink / raw)


Sergio <alguien@microsoft.com> wrote :
> Ada simply cannot do that, sorry.
...
> > void WorkFile(char *fileName)
> > {
> >     char Buffer[4096];
> >     FILE *handle;
> >
> >     handle = fopen(fileName, "r");
> >     while (!feof(handle))
> >     {
> >         fread(handle, 4096, 1, buffer); /* Read data from file to buffer
> */
> >         Work(Buffer);   /* ... do some work with such data */
> >     }
> > }

Not quite.  Aside from the fact that your
fread() arguments are backwards (and any
conforming C compiler is required to diagnose
it if you #include'd stdio.h as you should) --

Ada does make it _more difficult_
to incorrectly process garbage (or crash!)
if the input file is empty or nonexistent,
or process the last block twice otherwise.

I suspect the more serious answers already
given are what you intended to ask though.

--
- David.Thompson 1 now at worldnet.att.net








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

end of thread, other threads:[~2001-04-05  5:08 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <n5su6.24687$CL6.767652@telenews.teleline.es>
2001-03-26 14:08 ` Binary Data File - PLEASE HELP Sergio
2001-03-26 14:49   ` chris.danx
2001-03-26 14:56     ` Marin David Condic
2001-03-26 14:55   ` Marin David Condic
2001-03-26 19:51     ` Ted Dennison
2001-03-26 20:55       ` Marin David Condic
2001-03-26 21:16       ` tmoran
2001-03-26 22:16         ` Marin David Condic
2001-03-26 22:41         ` Ted Dennison
2001-03-26 14:56   ` Ted Dennison
2001-03-26 15:09     ` Marin David Condic
2001-03-26 15:21   ` Martin Dowie
2001-04-05  5:08   ` David Thompson

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