Differenze tra le versioni di "Fread ()"

Da Gambas-it.org - Wikipedia.
Riga 15: Riga 15:
 
  Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END
 
  Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END
 
   
 
   
 +
<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 fopen(__filename As String, __modes As String) As Pointer
 
   
 
   
  Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long
+
<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 <FONT color=#B22222>fwrite</font>(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
   
 
   
  Private Extern fseek(streamp As Pointer, offset As Long, whence As Integer) As Integer
+
<FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)''
 +
' ''Seek to a certain position on STREAM.''</font>
 +
  Private Extern <FONT color=#B22222>fseek</font>(__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 25: Riga 31:
 
  Private Extern <FONT color=#B22222>fread</font>(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long
 
  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 fclose (FILE *__stream)''
 +
' ''Close STREAM.''</font>
 
  Private Extern fclose(__stream As Pointer) As Integer
 
  Private Extern fclose(__stream As Pointer) As Integer
 
   
 
   
Riga 30: Riga 38:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
  Dim p, pw, pr As Pointer
+
  Dim p, pw, pr As Pointer
  Dim s As String
+
  Dim s As String
  Dim l As Long
+
  Dim l As Long
 
      
 
      
 
   s = "Testo qualsiasi"
 
   s = "Testo qualsiasi"
Riga 57: Riga 65:
 
   
 
   
 
  '''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:
 +
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>' ''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 l As Long = 7
 +
  Dim d, n, i As Integer
 +
 +
  s = "<FONT Color=gray>''/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), l, p)
 +
    i += n
 +
    Print "\e[0mByte letti in totale: \e[31m"; i, n
 +
  Until feof(p)  <FONT Color=gray>' ''...oppure:'' '''n < l'''</font>
 +
 +
  fclose(p)
 +
 +
<FONT Color=gray>' ''Libera la memoria precedentemente allocata e si assicura che il Puntatore non punti a un indirizzo rilevante di memoria:''</font>
 +
  Free(pr)
 +
  pr = 0
 +
 +
'''End'''
  
  

Versione delle 08:51, 1 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)
 
  fclose(p)
  
' Libera la memoria precedentemente allocata:
  Free(pw)
  Free(pr)

End

In quest'altro esempio si procederà a gestire la lettura a blocchi (ossia con una quantità fissa) di valori-byte da un 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

' 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 l As Long = 7
 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), l, p)
   i += n
   Print "\e[0mByte letti in totale: \e[31m"; i, n
 Until feof(p)   ' ...oppure: n < l

 fclose(p)

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

End


Note

[1] Possiamo intravvedere 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";