简体   繁体   English

如何以[Int]而不是String的形式读取文件?

[英]How to read file as [Int], not as String?

I need to read file as bytes, not as chars. 我需要将文件读取为字节,而不是字符。 But when I do getLine or getContents I get IO String, and need [Int], not String. 但是当我执行getLine或getContents时,我得到了IO String,并且需要[Int],而不是String。 What should I do? 我该怎么办?

There is Data.ByteString which has an internal representation of strict Word8 arrays. Data.ByteString具有严格的Word8数组的内部表示形式。

You use it pretty much as what you are already used to. 您几乎可以像以前一样使用它。 Import it qualified, eg 导入合格,例如

import qualified Data.ByteString as B

and then do stuff like 然后做类似的事情

main = do
  str <- B.getLine
  let bytes = B.unpack str
  print bytes

I am not sure how you got to believe that you need Int. 我不确定您如何相信自己需要Int。 You want a proper byte representation, eg Word8 . 您需要正确的字节表示形式,例如Word8

You likely are asking for some form of deserialization, which should be thought of as a separate step. 您可能正在要求某种形式的反序列化,应该将其视为一个单独的步骤。 First consider that you're reading in a binary file of some sort (ie it is not text). 首先考虑您正在读取某种二进制文件(即它不是文本)。 For this we probably want a bytestring and can do: 为此,我们可能需要一个bytestring并可以执行以下操作:

import qualified Data.ByteString.Lazy as BL
...
    contents <- BL.readFile someFile

After getting the contents of the file we can convert them from whatever form ( String , Text , ByteString etc). 获取文件内容后,我们可以将其转换为任何形式( StringTextByteString等)。 You say you want [Int] but that doesn't tell us what form the data is in - are these 32 bit integers in big endian? 您说您想要[Int]但这并不能告诉我们数据的格式是什么-big endian中的这些32位整数吗? 64 bits? 64位? How many are there, do you know? 有多少个,你知道吗? Lets assume 32 bit big endian ints... and lets assume the file consists purely of these values: 让我们假设32位大字节序整数...并让文件纯粹由以下值组成:

import qualified Data.ByteString.Lazy as BL
import Data.Binary.Get
import Control.Monad
import Foreign.Storable 
import Data.Word 

readAndDecode :: FilePath -> IO [Int]
readAndDecode someFile =
  do contents <- BL.readFile someFile
     let nrWord32 = fromIntegral (BL.length contents) `div` sizeOf (undefined :: Word32)
     return $ runGet (map fromIntegral `fmap` replicateM nrWord32 getWord32be) contents

That is, we read the file, compute the number of 32 bit words in the file, decode, and convert each Word32 into an Int 也就是说,我们读取文件,计算文件中32位字的数量,解码并将每个Word32转换为Int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM