Differenze tra le versioni di "Fwrite ()"

Da Gambas-it.org - Wikipedia.
Riga 20: Riga 20:
 
  Private Extern <FONT color=#B22222>fwrite</font>(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
  Private Extern <FONT color=#B22222>fwrite</font>(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
   
 
   
  Private Extern fseek(filestream As Pointer, offset As Long, whence As Integer) As Integer
+
  Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer
 
   
 
   
  Private Extern fread(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
+
  Private Extern fread(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long
 
   
 
   
  Private Extern fclose(filestream As Pointer) As Integer
+
  Private Extern fclose(__stream As Pointer) As Integer
 
   
 
   
 
   
 
   
Riga 40: Riga 40:
 
   p = fopen("/tmp/f", "w+")
 
   p = fopen("/tmp/f", "w+")
 
    
 
    
   <FONT color=#B22222>fwrite(pw, 1, len(s), p)</font>
+
   <FONT color=#B22222>fwrite</font>(pw, 1, len(s), p)
 
    
 
    
 
   fseek(p, 0, SEEK_SET)
 
   fseek(p, 0, SEEK_SET)

Versione delle 07:28, 9 set 2016

La funzione della libreria di C

size_t fwrite (const void *__ptr, size_t __size, size_t __n, FILE *__s)

scrive in un flusso n blocchi (nmemb) di dati, aventi ciascuno una dimensione di size byte, memorizzati nel buffer puntato da un Puntatore (ptr). Ritorna il numero di byte scritti nel 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 fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long In "libc:6"


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

Library "libc:6"

Private Const SEEK_SET As Integer = 0

Private Extern fopen(fl As String, mode 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

Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer

Private Extern fread(__ptr As Pointer, __size As Long, __n As Long, __stream As Pointer) As Long

Private Extern fclose(__stream 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, 0, SEEK_SET)

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

  free(pw)
  free(pr)

End