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

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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀