gpt4 book ai didi

c - 如何在文件上使用 SEEK_CUR*

转载 作者:太空狗 更新时间:2023-10-29 15:18:06 24 4
gpt4 key购买 nike

offset=ftell(ptr)-sizeof(student1);
fseek(ptr,offset,SEEK_SET);
fwrite(&student1,sizeof(student1),1,ptr);

这段C代码的意思是将指针从当前位置ftell(ptr)移动到刚刚读取的结构 block 的开始处。我说得对吗?

如果我是对的,我可以使用 SEEK_CUR 而不是 SEEK_SET 返回到文件中结构 block 的开头吗?

请告诉我如何使用 SEEK_CUR 并返回到结构 block 的开头。

我是编程新手。所以请帮助我。

编辑:感谢您的回答。我想做的是搜索关键字(学生的卷号)并更新该学生的信息(姓名、地址等)。更新后的数据成功替换了之前的数据。请让我再问一个问题。有什么方法可以将新数据插入到以前的数据之上,而不是用旧数据替换它?

最佳答案

This C code means move the pointer from the current position [ ftell(ptr) ] to start of the just read structure block. Am I right?

我也这么认为。

Please show me how to use SEEK_CUR and go backward to start of the structure block.

您可以使用负偏移量。

#include <stdio.h>

fseek (ptr, -sizeof student1, SEEK_CUR);

无论如何,你应该避免这些调用;它可能会很慢。使用顺序阅读。

关于c - 如何在文件上使用 SEEK_CUR*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13330464/

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