简体   繁体   English

覆盖文件内容linux系统调用

[英]Overwrite file content linux system call

I am able to append one file content to other using O_APPEND flag in "open" system call. 我可以在“打开”系统调用中使用O_APPEND标志将一个文件内容附加到其他文件。 but struggling a lot to overwrite the existing content.when I try opening the file with just O_WRONLY with out any flags its not overwriting the existing content. 但是要覆盖现有的内容却很费劲。当我尝试仅使用O_WRONLY打开文件而没有任何标志时,它不会覆盖现有的内容。 Can some please hep me in this? 有人可以帮我吗?

Either add O_TRUNC (if you want to toss out all of the previous stuff), or seek the fd to 0 (if you want to "insert" over it, like an insert cursor). 添加O_TRUNC(如果要抛弃所有先前的内容),或者将fd设置为0(如果要“插入”它,就像插入光标一样)。 Be aware that you might not be able to see changes immediately - that's because of buffered I/O. 请注意,由于缓冲的I / O,您可能无法立即看到更改。 You can use O_SYNC to make sure every write is flushed through. 您可以使用O_SYNC来确保每次写入都被刷新。 (Probably don't do that though.) (不过可能不这样做。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM