Differenze tra le versioni di "Fseek ()"

Da Gambas-it.org - Wikipedia.
 
(2 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
  int fseek(FILE *stream, long offset, int whence);
+
  int fseek(FILE *__stream, long int __off, int __whence)
 
posiziona il puntatore all'interno del flusso di dati ad un determinato byte.
 
posiziona il puntatore all'interno del flusso di dati ad un determinato byte.
  
Riga 14: Riga 14:
  
 
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:
 
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 <FONT color=#B22222>Extern fseek</font>(filestream As Pointer, offset As Long, whence As Integer) As Integer In "libc:6"
+
  Private <FONT color=#B22222>Extern fseek</font>(__stream As Pointer, __off As Long, __whence As Integer) As Integer In "libc:6"
  
  
Riga 27: Riga 27:
 
  Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
  Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
   
 
   
  <FONT color=gray>' ''int fseek(FILE *stream, long offset, int whence)''
+
  <FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)''
 
  ' ''Seek to a certain position on STREAM.''</font>
 
  ' ''Seek to a certain position on STREAM.''</font>
  Private Extern <FONT color=#B22222>fseek</font>(streamp As Pointer, offset As Long, whence As Integer) As Integer
+
  Private Extern <FONT color=#B22222>fseek</font>(__stream As Pointer, __off As Long, __whence As Integer) As Integer
 
   
 
   
 
  Private Extern fread(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
 
  Private Extern fread(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long
Riga 38: Riga 38:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
    
 
    
  Dim p, pw, pr As Pointer
+
  Dim p, pw, pr As Pointer
  Dim s As String = "Testo qualsiasi"
+
  Dim s As String = "Testo qualsiasi"
 
   
 
   
+
  <FONT color=gray>' ''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":''</font>
  <FONT color=gray>' ''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":''</font>
 
 
   pw = Alloc(s)
 
   pw = Alloc(s)
 
   pr = Alloc(len(s))
 
   pr = Alloc(len(s))
Riga 63: Riga 61:
 
   
 
   
 
  '''End'''
 
  '''End'''
 
 
  
  
Riga 74: Riga 70:
 
  Private Extern fopen(__filename As String, __modes As String) As Pointer
 
  Private Extern fopen(__filename As String, __modes As String) As Pointer
 
   
 
   
  <FONT color=gray>' ''int fseek(FILE *stream, long offset, int whence)''
+
  <FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)''
 
  ' ''Seek to a certain position on STREAM.''</font>
 
  ' ''Seek to a certain position on STREAM.''</font>
  Private Extern <FONT color=#B22222>fseek</font>(streamp As Pointer, offset As Long, whence As Integer) As Integer
+
  Private Extern <FONT color=#B22222>fseek</font>(__stream As Pointer, __off As Long, __whence As Integer) As Integer
 
   
 
   
 
  <FONT color=gray>' ''long int ftell (FILE *__stream)''
 
  <FONT color=gray>' ''long int ftell (FILE *__stream)''
 
  ' ''Return the current position of STREAM.''</font>
 
  ' ''Return the current position of STREAM.''</font>
  Private Extern <FONT color=#B22222>ftell</font>(filestream As Pointer) As Long
+
  Private Extern <FONT color=#B22222>ftell</font>(__stream As Pointer) As Long
 
   
 
   
 
  Private Extern fclose(__stream As Pointer) As Integer
 
  Private Extern fclose(__stream As Pointer) As Integer
Riga 87: Riga 83:
 
  '''Public''' Sub Main()
 
  '''Public''' Sub Main()
 
   
 
   
  Dim pf As Pointer
+
  Dim pf As Pointer
  Dim l as Long
+
  Dim l as Long
 
   
 
   
 
   pf = fopen("<FONT color=gray>''/percorso/del/file''</font>", "rb")
 
   pf = fopen("<FONT color=gray>''/percorso/del/file''</font>", "rb")

Versione attuale delle 08:55, 1 set 2022

La funzione della libreria di C

int fseek(FILE *__stream, long int __off, 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(__stream As Pointer, __off 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 Enum SEEK_SET = 0, SEEK_CUR, SEEK_END

Private Extern fopen(__filename As String, __modes As String) As Pointer

Private Extern fwrite(__ptr As Pointer, __size As Long, __n As Long, __s As Pointer) As Long

' int fseek(FILE *__stream, long int __off, int __whence)
' Seek to a certain position on STREAM.
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, __s 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


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

Library "libc:6"

Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END

Private Extern fopen(__filename As String, __modes As String) As Pointer

' int fseek(FILE *__stream, long int __off, int __whence)
' Seek to a certain position on STREAM.
Private Extern fseek(__stream As Pointer, __off As Long, __whence As Integer) As Integer

' long int ftell (FILE *__stream)
' Return the current position of STREAM.
Private Extern ftell(__stream As Pointer) As Long

Private Extern fclose(__stream 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