Fread (): differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 7: | Riga 7: | ||
Volendola utilizzare in Gambas, bisognerà dichiararla con ''Extern'', nonché bisognerà dichiarare la libreria di C: ''libc.so.6'', nella quale la funzione è contenuta: | Volendola utilizzare in Gambas, bisognerà dichiararla con ''Extern'', nonché bisognerà dichiarare la libreria di C: ''libc.so.6'', nella quale la funzione è contenuta: | ||
Private <FONT color=#B22222>Extern fread</font>(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long In "libc:6" | Private <FONT color=#B22222>Extern fread</font>(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long In "libc:6" | ||
| Riga 67: | Riga 66: | ||
'''End''' | '''End''' | ||
In quest'altro esempio si procederà a gestire la lettura a blocchi (ossia con una quantità fissa) di valori-byte da un file: | |||
Private Const BLOCCO As Integer = '''7''' | ===Leggere un file a blocchi di byte per volta=== | ||
In quest'altro esempio si procederà a gestire la lettura a blocchi (ossia con una quantità fissa) di valori-byte per volta da un file: | |||
Private Const BLOCCO As Integer = '''7''' <FONT Color=gray>' ''Ogni volta la funzione "fread()" legge 7 byte dal file''</font> | |||
Versione delle 08:10, 6 set 2022
La funzione della libreria di C
size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)
legge da un flusso n blocchi (__n) di dati, aventi ciascuno una dimensione di __size byte, memorizzandoli nel buffer puntato da un Puntatore (__ptr). [ Nota 1 ]
Ritorna il numero di byte letti dal flusso.
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 fread(__ptr As Pointer, __size As Long, __n As Long, __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"
Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END
' 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 fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)
' Write chunks of generic data to STREAM.
Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
' int fseek(FILE *__stream, long int __off, int __whence)
' Seek to a certain position on STREAM.
Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer
' 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 fclose (FILE *__stream)
' Close STREAM.
Private Extern fclose(__stream As Pointer) As Integer
Public Sub Main()
Dim p, pw, pr As Pointer
Dim s As String
Dim l As Long
s = "Testo qualsiasi"
l = len(s)
pw = Alloc(s)
pr = Alloc(SizeOf(gb.Byte) * i)
p = fopen("/tmp/f", "w+")
fwrite(pw, 1, l, p)
fseek(p, 0, SEEK_SET)
fread(pr, 1, l, p)
Print String@(pr)
' Libera la memoria precedentemente allocata:
fclose(p)
Free(pr)
Free(pw)
' Si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:
pr = 0
pw = 0
End
Leggere un file a blocchi di byte per volta
In quest'altro esempio si procederà a gestire la lettura a blocchi (ossia con una quantità fissa) di valori-byte per volta da un file:
Private Const BLOCCO As Integer = 7 ' Ogni volta la funzione "fread()" legge 7 byte dal file 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 ' 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 n = fread(pr, SizeOf(gb.Byte), BLOCCO, p) i += n Print "\e[0mByte letti in totale: \e[31m"; i, n Until feof(p) ' ...oppure: n == 0, oppure: n < BLOCCO, oppure: i == d, oppure: 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
Note
[1] Possiamo intravvedere quasi un'analogia di funzionamento fra le funzioni fread() e fwrite() ed i Metodi ".Read()" e ".Write()" dei vettori in Gambas.
Infatti analizzando il codice seguente:
Public Sub Main() Dim bb As Byte[] Dim fl, ex As File fl = Open "/percorso/del/file/da/leggere" For Read ex = Open "/percorso/del/file/da/scrivere" For Create With bb = New Byte[](Lof(fl)) .Read(fl, 0, Lof(fl)) .Write(ex, 0, Lof(fl)) End With fl.Close ex.Close End
possiamo infatti notare che:
- il buffer di tipo Puntatore, presente come primo parametro delle funzioni fread() e fwrite() di C, corrisponde alla variabile vettoriale "bb";
- il secondo parametro delle due funzioni esterne di C, corrisponde al terzo parametro dei metodi .read() e .write() della variabile vettoriale "bb";
- il quarto parametro delle due funzioni esterne di C, corrisponde sostanzialmente al primo parametro dei due metodi della variabile vettoriale "bb";