fsetpos 没有按预期设置文件指针的位置

fsetpos does not set the file pointer's position as expected

//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 更新 pos1pos2 变量,然后使用它们 - 通过 "a" 一些代码 - 作为参数fseek 函数中的偏移参数!这是一个非常好的想法,但它有两个缺点: 首先:pos1pos2 是一种奇怪的数据类型。我怎样才能将它们放入偏移量参数并且它们的数据类型不匹配。 第二:我喜欢治标不治本。效率低下。
  3. Post 这里。

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

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

编辑后更新:现在两个调用都是 fsetpos???。您需要 get 某处的位置,稍后再用 set 返回。