Differenze tra le versioni di "Lseek()"

Da Gambas-it.org - Wikipedia.
Riga 5: Riga 5:
 
<BR>* SEEK_CUR : l'offset del file viene impostato sulla posizione corrente più i byte di ''__offset''.
 
<BR>* SEEK_CUR : l'offset del file viene impostato sulla posizione corrente più i byte di ''__offset''.
 
<BR>* SEEK_END : l'offset del file è impostato sulla dimensione del file più i byte di ''__offset''.
 
<BR>* SEEK_END : l'offset del file è impostato sulla dimensione del file più i byte di ''__offset''.
 +
 +
In altri termini '''lseek()''' sposta la posizione del file del file-descriptor di ''_offset'' byte partendo all'inizio del file (se ''__whence'' è impostato a ''SEEK_SET'' ), alla posizione corrente (se ''__whence'' è impostato a ''SEEK_CUR'' ) o alla fine del file (se ''__whence'' è impostato a ''SEEK_END'' ).
  
 
Volendo usare la funzione esterna "lseek()" in Gambas, andrà così dichiarata:
 
Volendo usare la funzione esterna "lseek()" in Gambas, andrà così dichiarata:
Riga 21: Riga 23:
 
  <FONT color=gray>' ''int open(const char *__file, int __oflag, ...)''
 
  <FONT color=gray>' ''int open(const char *__file, int __oflag, ...)''
 
  ' ''Open FILE and return a new file descriptor for it.''</font>
 
  ' ''Open FILE and return a new file descriptor for it.''</font>
  Private Extern open_C(__file As String, oflag As Integer) As Integer <FONT color=#B22222>'''Exec "open"'''</font>
+
  Private Extern open_C(__file As String, oflag As Integer) As Integer Exec "open"
 
   
 
   
 
  <FONT color=gray>' ''__off_t lseek (int __fd, __off_t __offset, int __whence)''
 
  <FONT color=gray>' ''__off_t lseek (int __fd, __off_t __offset, int __whence)''

Versione delle 19:17, 1 nov 2022

La funzione di C lseek()

__off_t lseek (int __fd, __off_t __offset, int __whence);

riposiziona in lettura e in scrittura il puntatore associato al descrittore di file __fd nel 2° parametro formale __offset secondo il 3° parametro __whence, come segue:
* SEEK_SET : l'offset del file è impostato su byte di __offset.
* SEEK_CUR : l'offset del file viene impostato sulla posizione corrente più i byte di __offset.
* SEEK_END : l'offset del file è impostato sulla dimensione del file più i byte di __offset.

In altri termini lseek() sposta la posizione del file del file-descriptor di _offset byte partendo all'inizio del file (se __whence è impostato a SEEK_SET ), alla posizione corrente (se __whence è impostato a SEEK_CUR ) o alla fine del file (se __whence è impostato a SEEK_END ).

Volendo usare la funzione esterna "lseek()" in Gambas, andrà così dichiarata:

Private Extern lseek(__fd As Integer, __offset As Long, __whence As Integer) As Long In "libc:6"

Va precisato che la funzione esterna "lseek()" va usata in combinazione con la funzione esterna "open()" di C.

Esempio pratico, nel quale si conoscerà la dimensione del file caricato:

Library "libc:6"

Private Enum O_RDONLY = 0, O_WRONLY, O_RDWR, O_ACCMODE, O_CREAT = &100, O_APPEND = &2000
Private Const SEEK_SET As Integer = 0
Private Const SEEK_CUR As Integer = 1
Private Const SEEK_END As Integer = 2
  
' int open(const char *__file, int __oflag, ...)
' Open FILE and return a new file descriptor for it.
Private Extern open_C(__file As String, oflag As Integer) As Integer Exec "open"

' __off_t lseek (int __fd, __off_t __offset, int __whence)
' Move FD's file position to OFFSET bytes.
Private Extern lseek(__fd As Integer, __offset As Long, __whence As Integer) As Long

' int close(int _fd)
' Deallocates the file descriptor indicated by fildes.
Private Extern close_C(_fd As Integer) As Integer Exec "close"


Public Sub Main()

 Dim fd As Integer
 Dim l As Long

' Viene aperto in sola "lettura" un file:
 fd = open_C("/percorso/del/file", O_RDONLY)

' Si impostano i parametri per conoscere la dimensione in byte del file aperto:
 l = lseek(fd, 0, SEEK_END)

 Print l; " byte"

 close_C(fd)
    
End