read
函数定义如下:
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t len)
我想为此编写一个 Haskell FFI。这是我的尝试(无法编译):
foreign import ccall unsafe "read" c_read :: CInt -> Ptr () -> CSize -> IO CSsize
read :: Int -- ^ file descriptor
-> Int64 -- ^ Bytes to allocate
-> Word64 -- ^ Read upto this many bytes
-> IO (String, Int64)
read fd bytes len = do
allocaBytes (fromIntegral bytes) (\ptr -> do
size <- c_read (fromIntegral fd) ptr (fromIntegral len)
dat <- peekByteOff (castPtr ptr) (fromIntegral size) :: IO String
return (dat, (fromIntegral size))
)
它无法编译,因为没有用于 Storable 的 String
实例。关于如何为此编写 FFI 的任何提示?
最佳答案
Foreign.C.String是这里的传统解决方案。
关于linux - 为读取系统调用编写 Haskell FFI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40411122/