當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


haskell hSeek用法及代碼示例

Haskell語言IO模塊中函數hSeek的用法及代碼示例。

用法類型:

Handle -> SeekMode -> Integer -> IO ()

計算方式hSeekhdl模式我根據模式設置手柄hdl的位置。如果模式為:

 * AbsoluteSeek: the position of hdl is 
                 set to i.
 * RelativeSeek: the position of hdl is 
                 set to offset i from the current position.
 * SeekFromEnd:  the position of hdl is set 
                 to offset i from the end of the file.

偏移量以8位字節的形式給出。

錯誤報告:如果超出係統資源限製,則hSeek計算可能會失敗,並顯示:isPermissionError。

示例1:

文件:/tmp/foo.txt
 : 

Hello, world!
Good bye!
源碼:

import IO

main = do hdl <- openFile "/tmp/foo.txt" ReadMode
	  a <- hGetChar hdl
	  hSeek hdl AbsoluteSeek 4 
	  b <- hGetChar hdl
	  print (a,b)
	  c <- hGetChar hdl
	  hSeek hdl RelativeSeek 3
	  d <- hGetChar hdl
	  print (c,d)
	  e <- hGetChar hdl
	  hSeek hdl SeekFromEnd (-3)
	  f <- hGetChar hdl
	  print (e,f)


輸出:
('H','o')
         
輸出:
(',','r')
         
輸出:
('l','y')
         

注:本文由純淨天空篩選整理自 haskell hSeek。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。