gpt4 book ai didi

c - 一个文件上的 O_APPEND 标志使 read() 系统调用在其他文件上表现得很奇怪

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

我有一个 C 程序,它在目录中创建指定数量的文件 (name-myfiles)。然后删除所有文件。然后创建一个非常大的文件(名称 -appfile),追加它,截断它。以上几轮操作循环进行。

为了验证每个write,我从相同的偏移量读取目标文件,它已经写入了数据。如果测试不对大文件使用 O_APPEND 标志,则此验证部分(read())进行得非常顺利。但否则,read 开始表现出奇怪的行为。第一轮测试完成后,read 读取的字节数为 0 或小于缓冲区大小,或者字节数等于缓冲区大小,但两个缓冲区的内容不匹配。

最佳答案

给定 destflg=O_RDWR | O_APPEND | O_CREAT;,我假设两件事:

  1. 您正在以追加模式打开文件
  2. 您在 Linux 上运行。
Linux 上的

pwrite() 坏了。

来自 the Linux pwrite() man page :

BUGS

POSIX requires that opening a file with the O_APPEND flag should have no effect on the location at which pwrite() writes data. However, on Linux, if a file is opened with O_APPEND, pwrite() appends data to the end of the file, regardless of the value of offset.

关于c - 一个文件上的 O_APPEND 标志使 read() 系统调用在其他文件上表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49045988/

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