Differenze tra le versioni di "Fseek ()"

Da Gambas-it.org - Wikipedia.
Riga 21: Riga 21:
 
  Library "libc:6"
 
  Library "libc:6"
 
   
 
   
  Private Const SEEK_SET As Integer = 0
+
  Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END
 
   
 
   
  Private Extern fopen(fl As String, mode As String) As Pointer
+
  Private Extern fopen(__filename As String, __modes As String) As Pointer
 
   
 
   
  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 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 offset, int whence)''
 
  ' ''Seek to a certain position on STREAM.''</font>
 
  ' ''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 <FONT color=#B22222>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 Long, __n As Long, __s As Pointer) As Long
 
   
 
   
  Private Extern fclose(filestream As Pointer) As Integer
+
  Private Extern fclose(__stream As Pointer) As Integer
 
   
 
   
 
   
 
   
Riga 51: Riga 51:
 
   fwrite(pw, 1, len(s), p)
 
   fwrite(pw, 1, len(s), p)
 
    
 
    
   <FONT color=#B22222>fseek(p, 0, SEEK_SET)</font>
+
   <FONT color=#B22222>fseek</font>(p, 0, SEEK_SET)
 
   
 
   
 
   fread(pr, 1, len(s), p)
 
   fread(pr, 1, len(s), p)
Riga 63: Riga 63:
 
   
 
   
 
  '''End'''
 
  '''End'''
 +
  
  
Riga 69: Riga 70:
 
  Library "libc:6"
 
  Library "libc:6"
 
   
 
   
  Private Const SEEK_END As Integer = 2
+
  Private Enum SEEK_SET = 0, SEEK_CUR, SEEK_END
 
   
 
   
  Private Extern fopen(fl As String, mode 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 offset, int whence)''
 
  ' ''Seek to a certain position on STREAM.''</font>
 
  ' ''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 <FONT color=#B22222>fseek</font>(streamp As Pointer, offset As Long, whence As Integer) As Integer
 
   
 
   
  Private Extern ftell(filestream As Pointer) As Long
+
<FONT color=gray>' ''long int ftell (FILE *__stream)''
 +
' ''Return the current position of STREAM.''</font>
 +
  Private Extern <FONT color=#B22222>ftell</font>(filestream As Pointer) As Long
 
   
 
   
  Private Extern fclose(filestream As Pointer) As Integer
+
  Private Extern fclose(__stream As Pointer) As Integer
 
   
 
   
 
   
 
   
Riga 87: Riga 90:
 
   Dim l as Long
 
   Dim l as Long
 
   
 
   
   pf = fopen("''/percorso/del/file''", "rb")
+
   pf = fopen("<FONT color=gray>''/percorso/del/file''</font>", "rb")
 
   
 
   
   fseek(pf, 0, SEEK_END)
+
   <FONT color=#B22222>fseek</font>(pf, 0, SEEK_END)
 
   
 
   
   l = ftell(pf)
+
   l = <FONT color=#B22222>ftell</font>(pf)
 
   
 
   
 
   Print l
 
   Print l

Versione delle 08:23, 9 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 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 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 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 offset, int whence)
' Seek to a certain position on STREAM.
Private Extern fseek(streamp As Pointer, offset As Long, whence As Integer) As Integer

' long int ftell (FILE *__stream)
' Return the current position of STREAM.
Private Extern ftell(filestream 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