douhui9631 2017-06-22 12:22
浏览 155
已采纳

从文本文件中删除前N个字节

Is there any function call or simple way to delete first N bytes from a text file in golang? Assuming that the file is contentiously appended by various go-routines, simultaneously I want to delete first N bytes of file.

  • 写回答

1条回答 默认 最新

  • duanqi6274 2017-06-22 21:15
    关注

    You need to do f.Seek to jump over first bytes and than do regular reading, see example:

    package main
    
    import (
        "fmt"
        "io"
        "io/ioutil"
        "os"
    )
    
    func main() {
        f, err := os.Open(os.Args[1])            // open file from argument
        if err != nil {
            fmt.Println(err)
            return
        }
    
        var skipBytes int64 = 5                  // how many bytes to skip
    
        _, err = f.Seek(skipBytes, io.SeekStart) // skipping first bytes
        if err != nil {
            fmt.Println(err)
            return
        }
    
        buffer := make([]byte, 1024)               // allocating buffer to read
        for {
            n, err := f.Read(buffer)               // reading
            fmt.Print(string(buffer[:n]))          // writing to console
            if err != nil {
                fmt.Printf("Err: %v
    ", err)
                return
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用