在golang中截断文件

在golang中截断文件,第1张

概述截断文件时,似乎是在开头添加额外的零字节: configFile, err := os.OpenFile("./version.json", os.O_RDWR, 0666)defer configFile.Close()check(err)//some actions happen hereconfigFile.Truncate(0)configFile.Write(js)confi 截断文件时,似乎是在开头添加额外的零字节:

configfile,err := os.Openfile("./version.Json",os.O_RDWR,0666)defer configfile.Close()check(err)//some actions happen hereconfigfile.Truncate(0)configfile.Write(Js)configfile.Sync()

结果,文件具有我在开头用0字节的部分写的内容.

如何在没有前导零的情况下截断并完全重写文件?

解决方法 请参阅Truncate上的文档:

Truncate changes the size of the file. It does not change the I/O offset. If there is an error,it will be of type *PathError.

所以你还需要在写之前寻找文件的开头:

configfile.Truncate(0)configfile.Seek(0,0)
总结

以上是内存溢出为你收集整理的在golang中截断文件全部内容,希望文章能够帮你解决在golang中截断文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/langs/1267735.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-08
下一篇2022-06-08

发表评论

登录后才能评论

评论列表(0条)

    保存