Differenze tra le versioni di "Fread ()"

Da Gambas-it.org - Wikipedia.
Riga 15: Riga 15:
 
   
 
   
 
  Private Extern fwrite(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
 
  Private Extern fwrite(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
 +
 +
Private Extern fseek(filestream As Pointer, offset As Long, whence As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)''</font>
 
  <FONT color=gray>' ''size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)''</font>
 
  Private <FONT color=#B22222>Extern fread</font>(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
 
  Private <FONT color=#B22222>Extern fread</font>(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
 
Private Extern fseek(filestream As Pointer, offset As Long, whence As Integer) As Integer
 
 
   
 
   
 
  Private Extern fclose(filestream As Pointer) As Integer
 
  Private Extern fclose(filestream As Pointer) As Integer

Versione delle 11:33, 14 set 2014

La funzione della libreria di C

size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

legge da un flusso n blocchi (nmemb), aventi ciascuno una dimensione di size byte, memorizzandoli nel buffer puntato da un Puntatore (ptr). 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 Integer, nmemb As Integer, filestream As Pointer) As Integer In "libc:6"


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

Private Const SEEK_SET As Integer = 0

Private Extern fopen(fl As String, mode As String) As Pointer

Private Extern fwrite(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer

Private Extern fseek(filestream As Pointer, offset As Long, whence As Integer) As Integer

' size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
Private Extern fread(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer

Private Extern fclose(filestream As Pointer) As Integer


Public Sub Main()
 
 Dim p, pw, pr As Pointer
 Dim s As String = "Testo qualsiasi"


' Poiché la variabile contenente i dati da scrivere nel flusso, e la variabile, nella quale si dovranno memorizzare i dati letti dal flusso,
' sono di tipo "Puntatore", allora anche nel codice Gambas si dovranno passare delle variabili di tipo "Puntatore":
  pw = Alloc(s)
  pr = Alloc(len(s))
 
  p = fopen("/tmp/f", "w+")
 
  fwrite(pw, 1, len(s), p)
  
  fseek(p, SEEK_SET, 0)

  fread(pr, 1, len(s), p)
       
  Print String@(pr)
 
  fclose(p)

  free(pw)
  free(pr)

End