gpt4 book ai didi

function - os.O_TRUNC 的作用是什么?

转载 作者:行者123 更新时间:2023-12-02 17:10:34 25 4
gpt4 key购买 nike

func OpenFile(name string, flag int, perm FileMode) (*File, error)

f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE|O_TRUNC, 0755)

“O_TRUNC”在写入之前是否清空整个文件?“截断”是什么意思?

此外,函数ioutil.WriteFile()在写入之前是否清空整个文件?

最佳答案

之前使用“如果可能”这样的措辞对 os.O_TRUNC 的定义存在一些混淆 - 请参阅 here .

今天docs阅读:

O_TRUNC  int = syscall.O_TRUNC  // truncate regular writable file when opened.

所以

Does "O_TRUNC" empty the entire file before writing ?

是的。它本质上会破坏文件的内容 - 如果文件路径已经存在(并且是文件或现有文件的符号链接(symbolic link))。

类似来自ioutil.WriteFile docs :

... WriteFile truncates it before writing.

关于function - os.O_TRUNC 的作用是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58681331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com