Linux C 库中类似 SetFileLength() 的函数
Linux下的C库中有没有可以设置文件长度的函数? 在Windows下我知道有一个SetFileLength()函数。 如果没有,在不删除和重写文件的情况下缩短文件的最佳方法是什么?
Is there a function in the C Library under Linux which can set the length of a file? Under Windows I know there is a SetFileLength() function.
If there is not, what is the best way of shortening a file without deleting and rewriting it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用截断功能。
int truncate(const char *path, off_t length);
从手册页中:
“truncate() 和 ftruncate() 函数导致由路径命名或由 fd 引用的常规文件被截断为精确长度字节的大小。如果文件以前大于此大小,则额外的数据将丢失。如果文件以前较短,则扩展部分读取为空字节”。
You can use the truncate function.
int truncate(const char *path, off_t length);
From the man page:
"The truncate() and ftruncate() functions cause the regular file named by path or referenced by fd to be truncated to a size of precisely length bytes. If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended, and the extended part reads as null bytes"
从它的联机帮助页来看:
From its manpage: