douxiza9868 2016-06-08 10:20
浏览 71

从文件读取字节到内存

I am using bufio to append byte arrays to a file. When I use cat on it I get:

[176 0 0 0 4 0 0 0 1 0 0 0 57 12 127 47 4 0 0 0 1 0 0 0 215 136 47 0 64 0 0 0 2 0 0 0 57 0 97 0 53 0 52 0 100 0 98 0 99 0 52 0 98 0 100 0 100 0 49 0 54 0 99 0 49 0 97 0 49 0 57 0 56 0 48 0 52 0 55 0 53 0 49 0 100 0 49 0 49 0 51 0 99 0 52 0 52 0 98 0 24 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 99 0 108 0 105 0 101 0 110 0 116 0 0 0 36 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 111 0 116 0 104 0 101 0 114 0 32 0 112 0 97 0 114 0 116 0 110 0 101 0 114 0][176 0 0 0 4 0 0 0 1 0 0 0 4 162 127 47 4 0 0 0 1 0 0 0 215 136 47 0 64 0 0 0 2 0 0 0 57 0 97 0 53 0 52 0 100 0 98 0 99 0 52 0 98 0 100 0 100 0 49 0 54 0 99 0 49 0 97 0 49 0 57 0 56 0 48 0 52 0 55 0 53 0 49 0 100 0 49 0 49 0 51 0 99 0 52 0 52 0 98 0 24 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 99 0 108 0 105 0 101 0 110 0 116 0 0 0 36 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 111 0 116 0 104 0 101 0 114 0 32 0 112 0 97 0 114 0 116 0 110 0 101 0 114 0][176 0 0 0 4 0 0 0 1 0 0 0 35 200 127 47 4 0 0 0 1 0 0 0 215 136 47 0 64 0 0 0 2 0 0 0 57 0 97 0 53 0 52 0 100 0 98 0 99 0 52 0 98 0 100 0 100 0 49 0 54 0 99 0 49 0 97 0 49 0 57 0 56 0 48 0 52 0 55 0 53 0 49 0 100 0 49 0 49 0 51 0 99 0 52 0 52 0 98 0 24 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 99 0 108 0 105 0 101 0 110 0 116 0 0 0 36 0 0 0 2 0 0 0 83 0 111 0 109 0 101 0 32 0 111 0 116 0 104 0 101 0 114 0 32 0 112 0 97 0 114 0 116 0 110 0 101 0 114 0]

So arrays of bytes. Now I want to read the file into memory, so I use:

content, err := ioutil.ReadFile(filename)

But when I display the content variable I get a completely different output than the contents of the file (and one array instead of many). What's wrong?

  • 写回答

2条回答 默认 最新

  • duanre4421 2016-06-08 14:02
    关注

    You wrote it wrong to the file. Its human readable format, should be binary.

    My guess you used something like fmt.Fprintln(file, byte_slice) or variants.

    It must be fmt.Fprintf(file, "%s", byte_slice) or similar.

    Please, show us code how you write to the file.

    评论

报告相同问题?

悬赏问题

  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号