Fread (): differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| (25 versioni intermedie di uno stesso utente non sono mostrate) | |||
| Riga 1: | Riga 1: | ||
La funzione della libreria di C | La funzione della libreria di C | ||
size_t fread(void * | size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream) | ||
legge da un flusso n blocchi ('' | legge da un flusso n blocchi (''__n'') di dati, aventi ciascuno una dimensione di ''__size'' byte, memorizzandoli nel buffer puntato da un Puntatore (''__ptr''). <SUP>[[[#Note|nota 1]]]</sup> | ||
<BR>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: | 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>( | Private <FONT color=#B22222>Extern fread</font>(__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()'': | Semplice esempio uso in Gambas in combinazione con le funzioni ''fwrite()'', ''fseek()'' e ''fclose()'': | ||
Private | Library "libc:6" | ||
Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END | |||
Private Extern fopen( | <FONT color=gray>' ''FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)'' | ||
' ''Open a file and create a new stream for it.''</font> | |||
Private Extern fopen(__filename As String, __modes As String) As Pointer | |||
Private Extern fwrite( | <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> | |||
Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long | |||
<FONT color=gray>' '' | <FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)'' | ||
Private | ' ''Seek to a certain position on STREAM.''</font> | ||
Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer | |||
Private Extern | <FONT color=gray>' ''size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)'' | ||
' ''Read chunks of generic data from STREAM.''</font> | |||
Private Extern <FONT color=#B22222>fread</font>(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long | |||
Private Extern fclose( | <FONT Color=gray>' ''int fclose (FILE *__stream)'' | ||
' ''Close STREAM.''</font> | |||
Private Extern fclose(__stream As Pointer) As Integer | |||
Public Sub Main() | |||
Dim p, pw, pr As Pointer | Dim p, pw, pr As Pointer | ||
Dim s As String = "Testo qualsiasi" | 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) | |||
<FONT color=#B22222>fread(pr, 1, l, p)</font> | |||
Print String@(pr) | |||
<FONT Color=gray>' ''Libera la memoria precedentemente allocata:''</font> | |||
fclose(p) | |||
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 | |||
===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" | |||
<FONT color=gray>' ''FILE *fopen (const char *__restrict __filename, const char *__restrict __modes)'' | |||
' ''Open a file and create a new stream for it.''</font> | |||
Private Extern fopen(__filename As String, __modes As String) As Pointer | |||
<FONT color=gray>' ''size_t fread(void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __stream)'' | |||
' ''Read chunks of generic data from STREAM.''</font> | |||
Private Extern <FONT color=#B22222>fread</font>(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long | |||
<FONT color=gray>' ''int feof (FILE *__stream)'' | |||
' ''Return the EOF indicator for STREAM.''</font> | |||
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)'' | |||
' ''Close STREAM.''</font> | |||
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 = "<FONT Color=darkgreen>''/percorso/del/file/da/leggere''</font>" | |||
d = Stat(s).Size | |||
pr = Alloc(SizeOf(gb.Byte), d) | |||
p = fopen(s, "r") | |||
Repeat | |||
n = <FONT color=#B22222>fread</font>(pr, SizeOf(gb.Byte), BLOCCO, p) | |||
i += n | |||
Print "\e[0mByte letti in totale: \e[31m"; i, 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) | |||
Free(pr) | |||
<FONT Color=gray>' ''Si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:''</font> | |||
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. | |||
<BR>Infatti analizzando il codice seguente: | |||
Public Sub Main() | |||
Dim bb As Byte[] | |||
Dim fl, ex As File | |||
fl = Open "<FONT Color=darkgreen>''/percorso/del/file/da/leggere''</font>" For Read | |||
ex = Open "<FONT Color=darkgreen>''/percorso/del/file/da/scrivere''</font>" 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"; | |||
Versione attuale delle 16:24, 14 giu 2024
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";