Ftell ()

Da Gambas-it.org - Wikipedia.

La funzione della libreria di C

long int ftell (FILE *__stream)

restituisce la posizione corrente del puntatore del file rispetto all'inizio del file medesimo.

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

Private Extern ftell(__stream As Pointer) As Long In "libc:6"


Semplice esempio uso in Gambas in combinazione con le funzioni fwrite(), fseek() e fclose():

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

' long int ftell (FILE *__stream)
' Return the current position of STREAM.
Private Extern ftell(__stream As Pointer) As Long

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


Public Sub Main()

 Dim p, pr As Pointer
 Dim s As String
 Dim d, n, i As Integer

 s = "/percorso/del/file/da/leggere"
 d = Stat(s).Size
 pr = Alloc(SizeOf(gb.Byte), d)

 p = fopen(s, "r")

 Repeat 
   fread(pr, SizeOf(gb.Byte), SizeOf(gb.Byte), p)
   Print "\e[0mPosizione del puntatore del file: \e[31m"; ftell(p), "\e[0m - Valore letto: \e[34m"; Hex(Byte@(pr), 2)
   Wait 0.3
 Until ftell(p) == d

' Libera la memoria precedentemente allocata:
 fclose(p)
 Free(pr)
' Si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:
 pr = 0

End