-5
//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 

So, i suggest to my self these:

  1. use fseek. Why do i bother my self with this? No, I will not use it. If I will use it, I will need to write more than 50 lines of code just because of that. Inefficient.
  2. Use fseek but not like in suggestion #1. Use it along with fsetpos like this: fsetpos updates the pos1 & pos2 variables and then use them - by "a" some how code- as arguments for the offset argument in the fseek function! This is a very nice idea but it has two drawbacks: First: pos1 and pos2 are of a weird type of data. How can I get them into offset argument and their type of data does not match. Second: I am like trying to treat the problem from its leaves rather than its root. Inefficient.
  3. Post here.
4

1 回答 1

0

您的第二个fgetpos可能是fsetpos. 这将解释您评论的行为:

pos2 现在被一个新值替换,比如 18。

编辑后更新:现在两个电话都是fsetpos???。您需要到get某个位置,然后set稍后再返回。

于 2015-07-25T15:41:20.560 回答