Lseek(): differenze tra le versioni
Nessun oggetto della modifica |
Nessun oggetto della modifica |
||
| Riga 6: | Riga 6: | ||
<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 | In altri termini '''lseek()''' sposta la posizione di ''__fd'' del file 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: | ||
Versione delle 06:24, 2 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 di __fd del file 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