Differenze tra le versioni di "Fwrite ()"

Da Gambas-it.org - Wikipedia.
Riga 5: Riga 5:
  
 
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 fwrite</font>(ptr As Pointer, size As Integer, nmemb As Integer, s As Pointer) As Integer In "libc:6"
+
  Private <FONT color=#B22222>Extern fwrite</font>(const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s) As Integer In "libc:6"
  
  
Riga 16: Riga 16:
 
  Private Extern fopen(fl As String, mode As String) As Pointer
 
  Private Extern fopen(fl As String, mode As String) As Pointer
 
   
 
   
  <FONT color=gray>' ''size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream)''
+
  <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 <FONT color=#B22222>fwrite</font>(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
+
  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(filestream As Pointer, offset As Long, whence As Integer) As Integer

Versione delle 07:53, 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(const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s) As Integer 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(filestream As Pointer, offset 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 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, 0, SEEK_SET)

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

  free(pw)
  free(pr)

End