gpt4 book ai didi

c - 使用 `posix_fallocate` 创建的修剪文件

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:23 25 4
gpt4 key购买 nike

我已经使用我的 C 程序在我的 Linux 系统上打开了一个用于输出的文件,并且我已经让 posix_fallocate 为它预留了 bunch 磁盘空间。

比我需要的更多,因为我不知道我需要多少。

天真地,我希望当我关闭文件时,它会被修剪到我实际需要的空间。但是,当然,生活并不那么容易。

那么,如何为输出预留一堆空间,然后将其缩减为我实际使用的空间?

谢谢!

最佳答案

行为是预期的 - 引用 posix_fallocate 手册页:

   If  the  size  of  the  file  is less than offset+len, then the file is
increased to this size; otherwise the file size is left unchanged.

要在关闭前修复文件大小,请查看 ftruncate:

   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.

关于c - 使用 `posix_fallocate` 创建的修剪文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9519957/

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