Differenze tra le versioni di "Fseek ()"

Da Gambas-it.org - Wikipedia.
(Creata pagina con '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 ...')
 
 
(8 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 6: Riga 6:
  
 
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:
 
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_SET = 0 - Inizio del file;
SEEK_CUR = 1 - Corrente posizione del puntatore nel flusso del file;
+
* SEEK_CUR = 1 - Corrente posizione del puntatore nel flusso del file;
SEEK_END = 2 - fine del file.
+
* SEEK_END = 2 - fine del file.
 
 
 
La posizione effettiva, dalla quale partirà il puntatore del file, è individuata da: ''whence'' + ''offset'' .  
 
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:
+
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"
 
+
Semplice esempio uso in Gambas in combinazione con le funzioni "fwrite()", "fread()" e "fclose()":
 
 
 
 
Semplice esempio uso in Gambas in combinazione con le funzioni ''fwrite()'', ''fread()'' e ''fclose()'':
 
 
  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>
+
  <FONT color=gray>' ''int fseek(FILE *__stream, long int __off, 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 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 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
 
   
 
   
 
   
 
   
  '''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,''
+
  pw = Alloc(s)
' ''sono di tipo "Puntatore", allora anche nel codice Gambas si dovranno passare delle variabili di tipo "Puntatore":''</font>
+
  pr = Alloc(len(s))
  pw = Alloc(s)
 
  pr = Alloc(len(s))
 
 
    
 
    
  p = fopen("/tmp/f", "w+")
+
  p = fopen("/tmp/f", "w+")
 
    
 
    
  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)
 
          
 
          
  Print String@(pr)
+
  Print String@(pr)
 
    
 
    
  fclose(p)
+
  fclose(p)
 +
 +
  free(pw)
 +
  free(pr)
 +
 +
End
 +
 
 +
===Uso della funzione "ftell()" per conoscere la dimensione di un file===
 +
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
 +
 +
<FONT color=gray>' ''int fseek(FILE *__stream, long int __off, int __whence)''
 +
' ''Seek to a certain position on STREAM.''</font>
 +
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)''
 +
' ''Return the current position of STREAM.''</font>
 +
Private Extern <FONT color=#B22222>ftell</font>(__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("<FONT color=darkgreen>''/percorso/del/file''</font>", "rb")
 +
 +
  <FONT color=#B22222>fseek</font>(pf, 0, SEEK_END)
 +
 +
  l = <FONT color=#B22222>ftell</font>(pf)
 +
 +
  Print l
 
   
 
   
  free(pw)
+
  fclose(pf)
  free(pr)
 
 
   
 
   
  '''End'''
+
  End

Versione attuale delle 16:29, 14 giu 2024

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

Uso della funzione "ftell()" per conoscere la dimensione di un file

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