doumo0206 2016-07-28 09:13
浏览 720
已采纳

Golang:file.Seek和file.WriteAt不能按预期工作

I am trying to make a program which writes at provided offsets in the file, like i can start writing from 20th offset etc.

here is one of sample code i was using as reference

package main

import (
    "fmt"
    "io/ioutil"
    "os"
)

const (
    filename   = "sample.txt"
    start_data = "12345"
)

func printContents() {
    data, err := ioutil.ReadFile(filename)
    if err != nil {
        panic(err)
    }
    fmt.Println("CONTENTS:", string(data))
}

func main() {
    err := ioutil.WriteFile(filename, []byte(start_data), 0644)
    if err != nil {
        panic(err)
    }

    printContents()

    f, err := os.OpenFile(filename, os.O_RDWR, 0644)
    if err != nil {
        panic(err)
    }
    defer f.Close()

    if _, err := f.Seek(20, 0); err != nil {
        panic(err)
    }

    if _, err := f.WriteAt([]byte("A"), 15); err != nil {
        panic(err)
    }

    printContents()
}

But i am always getting the same file content which is beginning from start like

12345A

I tried changing the seek values to (0,0) and (20,0) and (10,1) randomly which results in same output

Also i tried changing WriteAt offset to other offset like 10, 20 but this also resulted in same.

I want to get a solution so that i can write at any specified position in file, suggest me what is wrong in this code.

  • 写回答

1条回答 默认 最新

  • doutan1905 2016-07-28 09:47
    关注

    It works as expected.

    After running your code, your "sample.txt" file content is (16 bytes):

    [49 50 51 52 53 0 0 0 0 0 0 0 0 0 0 65]
    

    try:

    package main
    
    import (
        "fmt"
        "io/ioutil"
    )
    
    const (
        filename   = "sample.txt"
        start_data = "12345"
    )
    
    func printContents() {
        data, err := ioutil.ReadFile(filename)
        if err != nil {
            panic(err)
        }
        fmt.Println(data)
    }
    
    func main() {
        printContents()
    }
    

    you need to write enough bytes first, the use WriteAt offset:
    e.g. edit :

    start_data = "1234567890123456789012345678901234567890"
    

    then test your code:

    package main
    
    import (
        "fmt"
        "io/ioutil"
        "os"
    )
    
    const (
        filename   = "sample.txt"
        start_data = "1234567890123456789012345678901234567890"
    )
    
    func printContents() {
        data, err := ioutil.ReadFile(filename)
        if err != nil {
            panic(err)
        }
        fmt.Println(string(data))
    }
    
    func main() {
        err := ioutil.WriteFile(filename, []byte(start_data), 0644)
        if err != nil {
            panic(err)
        }
    
        printContents()
    
        f, err := os.OpenFile(filename, os.O_RDWR, 0644)
        if err != nil {
            panic(err)
        }
        defer f.Close()
    
        if _, err := f.Seek(20, 0); err != nil {
            panic(err)
        }
    
        if _, err := f.WriteAt([]byte("A"), 15); err != nil {
            panic(err)
        }
    
        printContents()
    }
    

    output:

    1234567890123456789012345678901234567890
    123456789012345A789012345678901234567890
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?