gpt4 book ai didi

c - 是否可以在 linux 中创建一个固定大小的文件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:16:04 24 4
gpt4 key购买 nike

我需要一个函数来在 Linux 中创建一个固定大小的文件。 truncatefopen,fseek,fclose 之类的函数不是解决方案,因为它们会用零填充打开的文件,但这不是必需的,我没有时间这样做.那么有没有什么函数,只打开一个固定长度的文件而不填充缓冲区?

提前致谢。

最佳答案

系统调用truncate(2) 不会用零填充文件。它只是增加文件报告的大小并在其中留下漏洞。

当您读取它时,您确实得到了零,但这只是操作系统的一种便利。

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 shorter, it is extended, and the extended part reads as null bytes ('\0').

关于漏洞(来自 TLPI):

The existence of holes means that a file’s nominal size may be larger than the amount of disk storage it utilizes (in some cases, considerably larger).

文件系统和漏洞:

Rather than allocate blocks of null bytes for the holes in a file, the file system can just mark (with the value 0) appropriate pointers in the i-node and in the indirect pointer blocks to indicate that they don't refer to actual disk blocks.

作为Per Johansson注意,这取决于文件系统。

Most native UNIX file systems support the concept of file holes, but many nonnative file systems (e.g., Microsoft’s VFAT) do not. On a file system that doesn’t support holes, explicit null bytes are written to the file.

关于c - 是否可以在 linux 中创建一个固定大小的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7158443/

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