简体   繁体   English

fseek 和 fwrite 过去 EOF

[英]fseek and fwrite past EOF

Let us first assume I have a 20 byte binary file consisting of 5 integers.我们首先假设我有一个由 5 个整数组成的 20 字节二进制文件。 Now let us say that I tried to:现在让我们说我试图:

fseek(fp, 2*sizeof(int), SEEK_END)

and I tried to call:我试着打电话:

fwrite(&i, sizeof(int), 1, fp);

Is this even possible?这甚至可能吗?

If it is possible will the new binary file be of size 32 or will it simply be of size 24?如果可能的话,新的二进制文件的大小是 32 还是只是大小为 24? If is it of 32, what is the content of the binary file from position 20 to 27?如果是 32,从 position 20 到 27 的二进制文件的内容是什么?

According to this :据此

  • Library implementations are allowed to not meaningfully support SEEK_END ( therefore, code using it has no real standard portability ).允许库实现无意义地支持SEEK_END因此,使用它的代码没有真正的标准可移植性)。

And here :在这里

POSIX allows seeking beyond the existing end of file . POSIX 允许在现有文件结尾之外进行搜索 If an output is performed after this seek, any read from the gap will return zero bytes.如果在此查找之后执行 output,则从间隙中读取的任何内容都将返回零字节。 Where supported by the filesystem, this creates a sparse file .在文件系统支持的情况下,这会创建一个稀疏文件

On a my testsystem seeking past SEEK_END fills the remainder of the file with nullbytes upto the new file pointer.在我的测试系统上, seeking过去的SEEK_END用空字节填充文件的其余部分,直到新的文件指针。

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

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