gpt4 book ai didi

c - fsetpos 未按预期设置文件指针的位置

转载 作者:行者123 更新时间:2023-11-30 21:39:34 25 4
gpt4 key购买 nike

//This is an *abstract* from my code: 


FILE* fpointer;
fpos_t pos1,pos2;


// make a do-while loop

do{


// I am reading a text file word by word
c = fscanf(fpointer, "%s", temp);



/* a code *begins* here. I am doing parsing
stuff
stuff
stuff
*/

// Now I am going to store the file pointer's position somewhere in the text


fgetpos (fpointer , &pos1);
//let us say that pos1 is now equal to 7
// I am gonna store the value 7 into pos2. I will explain why later

pos2=pos1;

/* the parsing code continues here.
stuff
stuff
stuff
*/


// Now I want to get back to the position # 7


fgetpos (fpointer , &pos2);

// The problem is this after executing the previous line of code:
/*
pos2 is replaced now with a new value let us say 18.
I do not if this a real problem but the file pointer's position could not be
modified to get back to 7

I will list my research effort:
1- I looked up many examples in the internet. I always see them using the fsetpos twice. Mine is used twice in one loop... not sure if there is a mistake in using it like that?!
2- I saw some examples use 2 variables instead of one. for example pos1 and pos2 but it did not fix the problem.
3- There is a chance that I misunderstood the function that it really just stores the position of the file pointer but it probably cannot modify it. However
this argument is invalid because I saw many examples that they use to set the pointer into the beginning of the file (although my code wants to set it to the middle not the beginning).
4- Unexpected behavior and more info wanted.

*/

}while(/*stuff*/);
// End of the code format

所以,我向自己建议:

  1. 使用fseek 。我为什么要为此烦恼?不,我不会使用它。如果我要使用它,我就需要编写 50 多行代码。效率低下。
  2. 使用fseek但不像建议#1。与 fsetpos 一起使用像这样:fsetpos更新pos1 & pos2变量,然后使用它们 - 通过“a”某种代码 - 作为 fseek 中的偏移参数的参数功能!这是一个非常好的想法,但它有两个缺点:第一:pos1pos2是一种奇怪的数据类型。我怎样才能让它们进入偏移参数并且它们的数据类型不匹配。第二:我喜欢从根源上解决问题。效率低下。
  3. 在这里发帖。

最佳答案

您的第二个 fgetpos 可能是 fsetpos 的拼写错误。这可以解释您评论的行为:

pos2 is replaced now with a new value let us say 18.

编辑后更新:现在两个调用都是fsetpos???。您需要在某处获取位置,然后稍后使用set返回该位置。

关于c - fsetpos 未按预期设置文件指针的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31628074/

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