Lseek(): differenze tra le versioni
Vai alla navigazione
Vai alla ricerca
(Creata pagina con "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 '...") |
Nessun oggetto della modifica |
||
| Riga 40: | Riga 40: | ||
fd = open_C("<FONT Color=gray>''/percorso/del/file''</font>", O_RDONLY) | fd = open_C("<FONT Color=gray>''/percorso/del/file''</font>", O_RDONLY) | ||
<FONT Color=gray>' '' | <FONT Color=gray>' ''Si impostano i parametri per conoscere la dimensione in byte del file aperto:''</font> | ||
l = <FONT color=#B22222>lseek</font>(fd, 0, SEEK_END) | l = <FONT color=#B22222>lseek</font>(fd, 0, SEEK_END) | ||
Versione delle 19:03, 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.
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