开发者

After rewind(), can you make fprintf() write to end of file (e.g. complete overwrite)

开发者 https://www.devze.com 2023-04-04 04:09 出处:网络
In C, the rewind() call starts the next write at the front of the file. As I understand it, when I call fprintf(), it will write to the end of the string I am trying to write and no further开发者_运维

In C, the rewind() call starts the next write at the front of the file.

As I understand it, when I call fprintf(), it will write to the end of the string I am trying to write and no further开发者_运维知识库. If the existing file has data past the end of the string I am writing, this is not overwritten.

Is there a way to change this behavior so that rewind() can be used to effectively perform a quick overwrite of the entire file?


If you want to truncate the file to zero length, reopen it in write mode. If you want to truncate it while it's open, ftruncate should do it in POSIX systems. There are, I believe, _chsize and _ftruncate in Windows, which are similar.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号