Differenze tra le versioni di "Fwrite ()"

Da Gambas-it.org - Wikipedia.
 
(7 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 fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
+
  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.
 
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:
 
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, filestream As Pointer) As Integer In "libc:6"
+
  Private <FONT color=#B22222>Extern fwrite</font>(__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()'':
+
Semplice esempio uso in Gambas in combinazione con le funzioni ''fread()'', ''fseek()'' e ''fclose()'': <SUP>&#091;[[#Note|Nota 1]]&#093;</sup>
 
  Library "libc:6"
 
  Library "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>
+
  <FONT color=gray>' ''size_t fwrite (const void *__restrict __ptr, size_t __size, size_t __n, FILE *__restrict __s)''
  Private <FONT color=#B22222>Extern fwrite</font>(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
+
' ''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(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 29: Riga 30:
 
    
 
    
 
   Dim p, pw, pr As Pointer
 
   Dim p, pw, pr As Pointer
   Dim s As String = "Testo qualsiasi"
+
<FONT color=gray>' ''Al termine di una stringa alfanumerica è opportuno porre il valore aritmetico di zero (''null''),''
 +
' ''affinché usando poi la funzione "String@()" siano mostrati soltanto i valori precedenti quello zero:''</font>
 +
   Dim s As String = "Testo qualsiasi<FONT color=#B22222>\0</font>"
 
   
 
   
 
   
 
   
Riga 39: Riga 42:
 
   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, SEEK_SET, 0)
+
   fseek(p, 0, SEEK_SET)
 
   
 
   
 
   fread(pr, 1, len(s), p)
 
   fread(pr, 1, len(s), p)
Riga 53: Riga 56:
 
   
 
   
 
  '''End'''
 
  '''End'''
 +
 +
 +
 +
=Note=
 +
[1] L'istruzione "fwrite()" del C può essere agevolmente emulata in Gambas con l'uso dell'istruzione "Write()" seguita nel primo campo dalla variabile di tipo ''File'', nel secondo campo da una variabile di tipo ''Puntatore'' che punta ad un'area di memoria contenente i dati da scrivere nel flusso, e da un terzo campo che esplicita il numero di dati da scrivere:
 +
Write #file, puntatore, numero_di_dati

Versione attuale delle 16:46, 20 ago 2020

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(): [Nota 1]

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
' Al termine di una stringa alfanumerica è opportuno porre il valore aritmetico di zero (null),
' affinché usando poi la funzione "String@()" siano mostrati soltanto i valori precedenti quello zero:
 Dim s As String = "Testo qualsiasi\0"


' 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


Note

[1] L'istruzione "fwrite()" del C può essere agevolmente emulata in Gambas con l'uso dell'istruzione "Write()" seguita nel primo campo dalla variabile di tipo File, nel secondo campo da una variabile di tipo Puntatore che punta ad un'area di memoria contenente i dati da scrivere nel flusso, e da un terzo campo che esplicita il numero di dati da scrivere:

Write #file, puntatore, numero_di_dati