douwo1517 2017-05-04 13:53
浏览 370

golang:从文件中读取字符串,整数和字节

I have to read a PPM file with its data encrypted (metadata is not encrypted), using Go, and the file format is given to me containing:

  • The "P3" magic number (read as string)

  • Image width (read as integer)

  • Image height (read as integer)

  • Maximum color value (read as integer)

Then, I need to read the rest of the file is the encrypted bits, which I have to read as a single byte array/slice.

e.g.:

P6
480 360
255
�š��GHFHFI�GHFHFG~EG~EG~E
...
HFD{BR�Rz�y|�vxyyhlf%8&NFzx

What is a good way to read string and integers (the 4 initial metadata values) and the rest (encrypted part) as bytes from the file? It can be the most efficient, but the cleanest (less lines) is preferred.

  • 写回答

1条回答 默认 最新

  • douwei2966 2017-05-04 14:26
    关注

    You can use bufio.Reader to read the first 3 lines using the ReadLine or ReadString method and reading the rest of the file using the Read method.

    After you've read the first 3 lines you can use the strings package to split the second line, and then the strconv package to parse the strings containing numbers as integers.

    For example:

    r := bufio.NewReader(file)
    line1, err := r.ReadString('
    ')
    if err != nil {
        panic(err)
    }
    // repeat to read line 2 and 3
    
    size := strings.Split(line2, " ")
    width, err := strconv.Atoi(size[0])
    if err != nil {
        panic(err)
    }
    height, err := strconv.Atoi(size[1])
    if err != nil {
        panic(err)
    }
    // repeat with line 3
    

    Update:

    As mentioned in the comments by Adrian you can use bufio.Scanner together with the bufio.ScanWord SplitFunc to scan for the metadata.

    s := bufio.NewScanner(r)
    s.Split(bufio.ScanWords)
    
    var count int
    for s.Scan() && count < 4 {
        switch count {
        case 0:
            magic = s.Text()
        case 1:
            if width, err = strconv.Atoi(s.Text()); err != nil {
                return
            }
        case 2:
            if height, err = strconv.Atoi(s.Text()); err != nil {
                return
            }
        case 3:
            if color, err = strconv.Atoi(s.Text()); err != nil {
                return
            }
        }
        count++
    }
    

    https://play.golang.org/p/-rOJb_WlFf

    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿