Feof ()

Da Gambas-it.org - Wikipedia.

La funzione feof(), dichiarata nel file header "/usr/include/stdio.h" come segue:

int feof (FILE *__stream)

ritorna l'indicatore di fine file relativo al parametro __stream. In particolare se il puntatore interno al flusso __stream è giunto alla fine del file, la funzione ritorna un valore diverso da zero.

Volendola utilizzare direttamente in Gambas, bisognerà dichiararla con Extern, nonché dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern feof(__stream As Pointer) As Integer In "libc:6"


Mostriamo un semplice esempio pratico, nel quale si leggerà un file byte dopo byte:

Library "libc:6"

' FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)
' Open a file and create a new stream for it.
Private Extern fopen(__filename As String, __modes As String) As Pointer

' size_t fread (void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)
' Read chunks of generic data from STREAM.
Private Extern fread(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long

' int feof (FILE *__stream)
' Return the EOF indicator for STREAM.
Private Extern feof(__stream As Pointer) As Integer

' int fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(__stream As Pointer) As Integer


Public Sub Main()
 
 Dim ptr, p As Pointer
 Dim i As Integer
 
 p = fopen("/percorso/del/file", "r")
 
 ptr = Alloc(SizeOf(gb.Byte), 1)
 
 Do
   fread(ptr, SizeOf(gb.Byte), 1, p)
   If feof(p) Then Break
   Print Hex(Byte@(ptr), 2)
 Wend
 
 Free(ptr)
 fclose(p)
 
End


Riferimenti