comp.lang.ada
 help / color / mirror / Atom feed
From: "Martin Dowie" <martin.dowie@baesystems.com>
Subject: Re: Binary Data File - PLEASE HELP
Date: Mon, 26 Mar 2001 16:21:07 +0100
Date: 2001-03-26T16:21:07+01:00	[thread overview]
Message-ID: <3abf5c97$1@pull.gecm.com> (raw)
In-Reply-To: J3Iv6.56705$CL6.1997020@telenews.teleline.es

[-- 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 */
> >     }
> > }
> >
> >
> >
>
>





  parent reply	other threads:[~2001-03-26 15:21 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [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 [this message]
2001-04-05  5:08   ` David Thompson
replies disabled

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