C语言 如何在文件上使用 SEEK_CUR*

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13330464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 04:23:52  来源:igfitidea点击:

How to use SEEK_CUR on a FILE*

cfseek

提问by Min Naing Oo

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

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

这个 C 代码意味着将指针从当前位置移动ftell(ptr)到刚读取的结构块的开头。我对吗?

If I'm right, can I use SEEK_CURinstead of SEEK_SETto go back to start of the structure block in the file?

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

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

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

I'm newbie to programming. So please kindly help me.

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

Edit: Thank you for the answers. What I'm trying to do is to search for a keyword (Roll number of a student) and update this student's information (Name,Address,..). The updated datas are replaced the previous datas successfully. Please let me ask one more question. It there any way to insert the new data above the previous data instead of replacing it with old data?

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

回答by md5

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

此 C 代码表示将指针从当前位置 [ ftell(ptr) ] 移动到刚读取的结构块的开头。我对吗?

I think so.

我想是这样。

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

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

You can use a negative offset.

您可以使用负偏移量。

#include <stdio.h>

fseek (ptr, -sizeof student1, SEEK_CUR);

Anyway, you should avoid these calls; it could be very slow. Use rather sequential reading.

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

回答by trojanfoe

try:

尝试:

fseek(ptr, -sizeof(student1), SEEK_CUR);