comp.lang.ada
 help / color / mirror / Atom feed
* Binding to feof
@ 2004-08-27 18:14 Jeffrey Carter
  2004-08-27 21:07 ` Jerry van Dijk
  2004-08-29  0:15 ` Keith Thompson
  0 siblings, 2 replies; 9+ messages in thread
From: Jeffrey Carter @ 2004-08-27 18:14 UTC (permalink / raw)


I've been experimenting with a binding to C's lightweight I/O library 
(fopen, fclose, feof, fgetc, fgets, fputc, fputs). Everything seems to 
work fine except feof, which always returns zero (False).

I'm using GNAT 3.15p/Win98.

Has anyone else experienced this? Does anyone have an explanation? If 
it's a C library error, is it fixed in later versions?

Thanks for any assistance.

-- 
Jeff Carter
"Have you gone berserk? Can't you see that that man is a ni?"
Blazing Saddles
38




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

* Re: Binding to feof
  2004-08-27 18:14 Binding to feof Jeffrey Carter
@ 2004-08-27 21:07 ` Jerry van Dijk
  2004-08-28  1:48   ` Jeffrey Carter
  2004-08-29  0:15 ` Keith Thompson
  1 sibling, 1 reply; 9+ messages in thread
From: Jerry van Dijk @ 2004-08-27 21:07 UTC (permalink / raw)



Jeffrey Carter <spam@spam.com> writes:

> I've been experimenting with a binding to C's lightweight I/O library (fopen,
> fclose, feof, fgetc, fgets, fputc, fputs). Everything seems to work fine
> except feof, which always returns zero (False).

If the binding is correct, it should work. Could you show an example of the
code and how you use it ?

I am not sure why you would want to do this though. If you want to
use/interface to  the C stream API, GNAT already provides a binding. If you 
are looking for the most efficient code, you need to use the Win32 file API, 
as the C API, on Windows, is itself also a binding to the Win32 API.

-- 
--  Jerry van Dijk
--  Leiden, Holland



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

* Re: Binding to feof
  2004-08-27 21:07 ` Jerry van Dijk
@ 2004-08-28  1:48   ` Jeffrey Carter
  2004-08-28  2:25     ` Steve
  2004-08-28 10:26     ` Eric Jacoboni
  0 siblings, 2 replies; 9+ messages in thread
From: Jeffrey Carter @ 2004-08-28  1:48 UTC (permalink / raw)


Jerry van Dijk wrote:
> 
> If the binding is correct, it should work. Could you show an example
> of the code and how you use it ?

function End_Of_File (File : File_Handle := Standard_Input)
return Boolean is
    Result : Int;

    function C_EOF (File : File_Handle) return Int;
    pragma Import (C, C_EOF, "feof");
    -- This seems to always return zero, even when at the EOF.
begin -- End_Of_File
    Result := C_EOF (File);

    return Result /= 0;
end End_Of_File;

Including debug statements indicates that Result is always zero.

> I am not sure why you would want to do this though. If you want to 
> use/interface to  the C stream API, GNAT already provides a binding.
> If you are looking for the most efficient code, you need to use the
> Win32 file API, as the C API, on Windows, is itself also a binding to
> the Win32 API.

None of the above. I don't want to use C streams, nor am I interested in 
the most efficient code. I just want to experiment with text I/O that 
doesn't count columns, lines, and pages, to see how it compares.

-- 
Jeff Carter
"Have you gone berserk? Can't you see that that man is a ni?"
Blazing Saddles
38




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

* Re: Binding to feof
  2004-08-28  1:48   ` Jeffrey Carter
@ 2004-08-28  2:25     ` Steve
  2004-08-28 18:51       ` Jeffrey Carter
  2004-08-28 10:26     ` Eric Jacoboni
  1 sibling, 1 reply; 9+ messages in thread
From: Steve @ 2004-08-28  2:25 UTC (permalink / raw)


I just did a quick check on the "C" definition of feof:

  int feof(FILE *stream)

It seems that the function requires a pointer to a stream.
Is your "FILE" type defined as a pointer?

Perhaps you need to have:
  function End_Of_File( File : access File_Handle :=
Standard_Input'Access );

Or something similar.

Steve
(The Duck)

"Jeffrey Carter" <spam@spam.com> wrote in message
news:AvRXc.518$8d1.147@newsread2.news.pas.earthlink.net...
> Jerry van Dijk wrote:
> >
> > If the binding is correct, it should work. Could you show an example
> > of the code and how you use it ?
>
> function End_Of_File (File : File_Handle := Standard_Input)
> return Boolean is
>     Result : Int;
>
>     function C_EOF (File : File_Handle) return Int;
>     pragma Import (C, C_EOF, "feof");
>     -- This seems to always return zero, even when at the EOF.
> begin -- End_Of_File
>     Result := C_EOF (File);
>
>     return Result /= 0;
> end End_Of_File;
>
> Including debug statements indicates that Result is always zero.
>
> > I am not sure why you would want to do this though. If you want to
> > use/interface to  the C stream API, GNAT already provides a binding.
> > If you are looking for the most efficient code, you need to use the
> > Win32 file API, as the C API, on Windows, is itself also a binding to
> > the Win32 API.
>
> None of the above. I don't want to use C streams, nor am I interested in
> the most efficient code. I just want to experiment with text I/O that
> doesn't count columns, lines, and pages, to see how it compares.
>
> -- 
> Jeff Carter
> "Have you gone berserk? Can't you see that that man is a ni?"
> Blazing Saddles
> 38
>





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

* Re: Binding to feof
  2004-08-28  1:48   ` Jeffrey Carter
  2004-08-28  2:25     ` Steve
@ 2004-08-28 10:26     ` Eric Jacoboni
  1 sibling, 0 replies; 9+ messages in thread
From: Eric Jacoboni @ 2004-08-28 10:26 UTC (permalink / raw)


Jeffrey Carter <spam@spam.com> writes:

> Jerry van Dijk wrote:
>> If the binding is correct, it should work. Could you show an example
>> of the code and how you use it ?
>
> function End_Of_File (File : File_Handle := Standard_Input)

feof waits for a FILE *

I would try :

type C_FILE is new System.Address;

function feof(fic : in C_FILE) return integer;
pragma import(C, feof);

-- 
�ric Jacoboni, n� il y a 1397132675 secondes



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

* Re: Binding to feof
  2004-08-28  2:25     ` Steve
@ 2004-08-28 18:51       ` Jeffrey Carter
  0 siblings, 0 replies; 9+ messages in thread
From: Jeffrey Carter @ 2004-08-28 18:51 UTC (permalink / raw)


Steve wrote:

> I just did a quick check on the "C" definition of feof:
> 
>   int feof(FILE *stream)
> 
> It seems that the function requires a pointer to a stream.
> Is your "FILE" type defined as a pointer?

Yes. File_Handle is an access type, with pragma Convention C. It's what 
I pass to all the other functions, which also take FILE* parameters, and 
they work fine.

-- 
Jeff Carter
"My brain hurts!"
Monty Python's Flying Circus
21




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

* Re: Binding to feof
  2004-08-27 18:14 Binding to feof Jeffrey Carter
  2004-08-27 21:07 ` Jerry van Dijk
@ 2004-08-29  0:15 ` Keith Thompson
  2004-08-30  6:56   ` Jeffrey Carter
  1 sibling, 1 reply; 9+ messages in thread
From: Keith Thompson @ 2004-08-29  0:15 UTC (permalink / raw)


Jeffrey Carter <spam@spam.com> writes:
> I've been experimenting with a binding to C's lightweight I/O library
> (fopen, fclose, feof, fgetc, fgets, fputc, fputs). Everything seems to
> work fine except feof, which always returns zero (False).

Keep in mind that C's feof() function returns true *after* an
attempted input operation has failed by reaching the end of the file;
Ada's End_Of_File function returns true if the *next* attempted input
will fail by reaching the end of the file.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
We must do something.  This is something.  Therefore, we must do this.



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

* Re: Binding to feof
  2004-08-29  0:15 ` Keith Thompson
@ 2004-08-30  6:56   ` Jeffrey Carter
  2004-08-30 17:16     ` Hyman Rosen
  0 siblings, 1 reply; 9+ messages in thread
From: Jeffrey Carter @ 2004-08-30  6:56 UTC (permalink / raw)


Keith Thompson wrote:

> Keep in mind that C's feof() function returns true *after* an
> attempted input operation has failed by reaching the end of the file;
> Ada's End_Of_File function returns true if the *next* attempted input
> will fail by reaching the end of the file.

That would explain it. That seems to make the function useless, since 
the other operations in my binding raise an exception if an operation 
fails by reaching the end of file.

-- 
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus
22




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

* Re: Binding to feof
  2004-08-30  6:56   ` Jeffrey Carter
@ 2004-08-30 17:16     ` Hyman Rosen
  0 siblings, 0 replies; 9+ messages in thread
From: Hyman Rosen @ 2004-08-30 17:16 UTC (permalink / raw)


Jeffrey Carter wrote:
> That would explain it. That seems to make the function useless, since 
> the other operations in my binding raise an exception if an operation 
> fails by reaching the end of file.

You cannot have anticipatory feof from the keyboard without actually
trying to read data and perhaps blocking, and in C, keyboard and file
I/O use the same mechanism. So instead, feof was designed to be after
the fact, so that when a read fails, you can check whether it was due
to EOF or some other reason.



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

end of thread, other threads:[~2004-08-30 17:16 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-27 18:14 Binding to feof Jeffrey Carter
2004-08-27 21:07 ` Jerry van Dijk
2004-08-28  1:48   ` Jeffrey Carter
2004-08-28  2:25     ` Steve
2004-08-28 18:51       ` Jeffrey Carter
2004-08-28 10:26     ` Eric Jacoboni
2004-08-29  0:15 ` Keith Thompson
2004-08-30  6:56   ` Jeffrey Carter
2004-08-30 17:16     ` Hyman Rosen

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