//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
だから、私は自分自身にこれらを提案します:
- 使用して
fseek
ください。なぜ私はこれで自分を悩ませるのですか?いいえ、使用しません。これを使うとしたら、それだけで 50 行以上のコードを書く必要があります。非効率的な。 - 使用します
fseek
が、提案#1とは異なります。fsetpos
このように一緒に使用してください: &変数をfsetpos
更新してから、関数のオフセット引数の引数として "a" 何らかのコードで使用します! これはとても良いアイデアですが、2 つの欠点があります。それらをオフセット引数に入れるにはどうすればよいですか。データの型が一致しません。第二に、問題を根本からではなく、葉から扱おうとしているようなものです。非効率的な。pos1
pos2
fseek
pos1
pos2
- ここに投稿してください。