Differenze tra le versioni di "Fread ()"
(6 versioni intermedie di uno stesso utente non sono mostrate) | |||
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 21: | Riga 20: | ||
<FONT color=gray>' ''size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)'' | <FONT color=gray>' ''size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)'' | ||
' ''Write chunks of generic data to STREAM.''</font> | ' ''Write chunks of generic data to STREAM.''</font> | ||
− | Private Extern | + | Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long |
<FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)'' | <FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)'' | ||
' ''Seek to a certain position on STREAM.''</font> | ' ''Seek to a certain position on STREAM.''</font> | ||
− | Private Extern | + | Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer |
<FONT color=gray>' ''size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)'' | <FONT color=gray>' ''size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)'' | ||
Riga 58: | Riga 57: | ||
Print String@(pr) | Print String@(pr) | ||
− | + | <FONT Color=gray>' ''Libera la memoria precedentemente allocata:''</font> | |
− | + | fclose(p) | |
− | <FONT | + | Free(pr) |
− | + | Free(pw) | |
− | + | <FONT Color=gray>' ''Si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:''</font> | |
+ | pr = 0 | ||
+ | pw = 0 | ||
'''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: | + | |
+ | ===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 in un ciclo da un file: | ||
+ | Private Const BLOCCO As Integer = '''7''' <FONT Color=gray>' ''Ogni volta la funzione "fread()" legge 7 byte dal file''</font> | ||
+ | |||
+ | |||
Library "libc:6" | Library "libc:6" | ||
Riga 79: | Riga 85: | ||
' ''Return the EOF indicator for STREAM.''</font> | ' ''Return the EOF indicator for STREAM.''</font> | ||
Private Extern feof(__stream As Pointer) As Integer | Private Extern feof(__stream As Pointer) As Integer | ||
+ | |||
+ | <FONT color=gray>' ''long int ftell (FILE *__stream)'' | ||
+ | ' ''Return the current position of STREAM.''</font> | ||
+ | Private Extern ftell(__stream As Pointer) As Long | ||
<FONT Color=gray>' ''int fclose (FILE *__stream)'' | <FONT Color=gray>' ''int fclose (FILE *__stream)'' | ||
Riga 89: | Riga 99: | ||
Dim p, pr As Pointer | Dim p, pr As Pointer | ||
Dim s As String | Dim s As String | ||
− | |||
Dim d, n, i As Integer | Dim d, n, i As Integer | ||
Riga 100: | Riga 109: | ||
Repeat | Repeat | ||
− | n = <FONT color=#B22222>fread</font>(pr, SizeOf(gb.Byte), | + | n = <FONT color=#B22222>fread</font>(pr, SizeOf(gb.Byte), BLOCCO, p) |
i += n | i += n | ||
Print "\e[0mByte letti in totale: \e[31m"; i, n | Print "\e[0mByte letti in totale: \e[31m"; i, n | ||
− | Until feof(p) <FONT Color=gray>' ''...oppure:'' '''n < | + | Until feof(p) <FONT Color=gray>' ''...oppure:'' '''n == 0''''', oppure:'' '''n < BLOCCO''''', oppure:'' '''i == d''''', oppure:'' '''ftell(p) == d'''</font> |
+ | <FONT Color=gray>' ''Libera la memoria precedentemente allocata:''</font> | ||
fclose(p) | fclose(p) | ||
− | |||
− | |||
Free(pr) | Free(pr) | ||
+ | <FONT Color=gray>' ''Si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:''</font> | ||
pr = 0 | pr = 0 | ||
Riga 116: | Riga 125: | ||
=Note= | =Note= | ||
− | [1] Possiamo intravvedere un'analogia di funzionamento fra le funzioni ''fread()'' e ''fwrite()'' ed i | + | [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: | Infatti analizzando il codice seguente: | ||
Riga 124: | Riga 133: | ||
Dim fl, ex As File | 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''' | '''End''' |
Versione attuale 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 in un ciclo 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";