Differenze tra le versioni di "Fseek ()"

Da Gambas-it.org - Wikipedia.
Riga 27: Riga 27:
 
  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
 
   
 
   
  <FONT color=gray>' ''int fseek(FILE *stream, long offset, int whence)''</font>
+
  <FONT color=gray>' ''int fseek(FILE *stream, long offset, int whence)''
  Private <FONT color=#B22222>Extern fseek</font>(filestream As Pointer, offset As Long, whence As Integer) As Integer
+
' ''Seek to a certain position on STREAM.''</font>
 +
  Private <FONT color=#B22222>Extern fseek</font>(streamp 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 fread(ptr As Pointer, size As Integer, nmemb As Integer, filestream As Pointer) As Integer
Riga 72: Riga 73:
 
  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>' ''int fseek(FILE *stream, long offset, int whence)''</font>
+
  <FONT color=gray>' ''int fseek(FILE *stream, long offset, int whence)''
  Private <FONT color=#B22222>Extern fseek</font>(filestream As Pointer, offset As Long, whence As Integer) As Integer
+
' ''Seek to a certain position on STREAM.''</font>
 +
  Private <FONT color=#B22222>Extern fseek</font>(streamp As Pointer, offset As Long, whence As Integer) As Integer
 
   
 
   
 
  Private Extern ftell(filestream As Pointer) As Long
 
  Private Extern ftell(filestream As Pointer) As Long

Versione delle 16:56, 8 set 2016

La funzione della libreria di C

int fseek(FILE *stream, long offset, int whence);

posiziona il puntatore all'interno del flusso di dati ad un determinato byte.

Il parametro offset indica il numero di byte di spostamento del puntatore del flusso dalla posizione del parametro whence.

Il parametro whence rappresenta il punto dal quale verrà aggiunto il valore di spostamento definito dal parametro offset. Esso può assumere uno di questi valori:

  • SEEK_SET = 0 - Inizio del file;
  • SEEK_CUR = 1 - Corrente posizione del puntatore nel flusso del file;
  • SEEK_END = 2 - fine del file.

La posizione effettiva, dalla quale partirà il puntatore del file, è individuata da: whence + offset .


Volendo utilizzare la funzione esterna fseek() in Gambas, bisognerà dichiararla con Extern, nonché bisognerà dichiarare la libreria di C: libc.so.6, nella quale la funzione è contenuta:

Private Extern fseek(filestream As Pointer, offset As Long, whence As Integer) As Integer In "libc:6"


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

Library "libc:6"

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

' int fseek(FILE *stream, long offset, int whence)
' Seek to a certain position on STREAM.
Private Extern fseek(streamp 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


In quest'altro esempio si utilizzerà anche la funzione ftell() per conoscere la dimensione in byte di un file:

Library "libc:6"

Private Const SEEK_END As Integer = 2

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

' int fseek(FILE *stream, long offset, int whence)
' Seek to a certain position on STREAM.
Private Extern fseek(streamp As Pointer, offset As Long, whence As Integer) As Integer

Private Extern ftell(filestream As Pointer) As Long

Private Extern fclose(filestream As Pointer) As Integer


Public Sub Main()

 Dim pf As Pointer
 Dim l as Long

  pf = fopen("/percorso/del/file", "rb")

  fseek(pf, 0, SEEK_END)

  l = ftell(pf)

  Print l

  fclose(pf)

End