-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.
Gold_Sky
  • 103
  • 4
  • 3
    You're not calling fsetpos in the code you posted... make sure you post actual, readable code that we can reproduce your problem with. – Mat Jul 25 '15 at 15:30
  • I meant to call it -_- – Gold_Sky Jul 25 '15 at 15:32
  • 4
    What do you mean by "I will need to write more than 50 lines of code just because of that"? Calling `fseek` takes one line, not 50. – melpomene Jul 25 '15 at 15:33
  • I mean I need to change/add (to) my logic. The problem with fseek is this: I am doing pasring word by word and I go back and forth in the text with various positions. Thus, if i will use it, I need to know where there position is in the file. This means that I need to use a counter inside my logic to know where I am reading and where I left off. So, why do I need to create a new block of logic and to add/change my logic just because I want to know where my position in the file while I can get where my position is by just using the fsetpos ?! – Gold_Sky Jul 25 '15 at 15:40
  • @Gold_Sky You're not making any sense. `fgetpos`/`fsetpos` correspond to `ftell`/`fseek`. Neither requires a separate counter. To know your position, you call `fgetpos` or `ftell` as required. – melpomene Jul 25 '15 at 15:41
  • 1
    @Gold_Sky Your latest edit changed both `fgetpos` calls to `fsetpos`, so now you're using uninitialized variables. – melpomene Jul 25 '15 at 15:43
  • Sad edits .You edited you question and corrected it . This should not be done . – ameyCU Jul 25 '15 at 15:44
  • Would it be appropriate to rollback that last silly edit? I only recently gained the rep to do stuff like that. – Peter Cordes Jul 25 '15 at 15:47
  • @Peter Cordes I will get it back. Look, I did not know that there is a function called fgetpos. Simple enough! I thought it is a typo in such a way that it is a typo but at the same time it is not a C library-defined function!!!!!!!! Now, I am glad that you guys told me. – Gold_Sky Jul 25 '15 at 15:50
  • @ameyCU see my response to Peter. – Gold_Sky Jul 25 '15 at 15:51
  • @Gold_Sky Such edits can misguide . But anyway see the link I provided in comment after answer to know about these functions. – ameyCU Jul 25 '15 at 15:53
  • 1
    The reason this question got so many downvotes is that 10 seconds with google for `fgetpos` tells you that it's used in combination with `fsetpos`, to get/set positions. All of the first 5 hits for "fgetpos" mention using it with `fsetpos`. – Peter Cordes Jul 25 '15 at 15:56
  • @Peter Cordes i just noticed (NOW) that the first hit mentions it in ONE TINY line of code. I thought they call fsetpos twice: one to get the position and the second call is to modify. So, it is a binary consecutive-defined operation. I mean one to get and one to modify!!!!!! which is wrong of course. The first hit was the only one that I opened and I built all of this based on it. I really did not notice that there was a line says fgetpos. No excuse. – Gold_Sky Jul 25 '15 at 16:18
  • correction: I also opened another one and whenever i found the letter "f" (as if it in a general function body styling shape), my brain reads it straightforward as fsetpos. – Gold_Sky Jul 25 '15 at 16:21
  • @melpomene I was willing to say: "according to my information: NO. You are wrong because fsetpos is called twice one to get the position and one to modify". But, -after reading carefully the links posted by the other members-yes you were correct. fgetpos is as ftell and and likewise for the fseek function. So, your point is precisely correct. – Gold_Sky Jul 25 '15 at 16:25

1 Answers1

0

Your 2nd fgetpos is probably a typo for fsetpos. That would explain the behaviour you comment on:

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

update after your edit: now both calls are fsetpos???. You need to get the position somewhere, and then go back to it with set later.

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • I really did not know that there is a function called fgetpos although I searched a lot. I thought it was really a typo HERE (and i fixed my code HERE) and I thought in my code (in my system) has fsetpos but I just got back it is not. Very impressive! My question will benifit any body who does not know that fgetpos is a function!!!! – Gold_Sky Jul 25 '15 at 15:46
  • @Gold_Sky May be you should read about these functions . Read from here-http://www.tutorialspoint.com/c_standard_library/c_function_fgetpos.htm – ameyCU Jul 25 '15 at 15:47
  • 1
    They're both documented in the same man page... http://linux.die.net/man/3/fgetpos – Peter Cordes Jul 25 '15 at 15:48
  • When I first asked and goolged, i settled with this function: fsetpos. Then I googled it itsef, i found this: http://www.tutorialspoint.com/c_standard_library/c_function_fsetpos.htm – Gold_Sky Jul 25 '15 at 15:57
  • But I admit it. I did not do that much of research regarding "file" functions and this is because I settled first with fsetpos. I really did not know about fgetpos. – Gold_Sky Jul 25 '15 at 15:58
  • Well go read that link you found, it *does* explain how to use get/set as a pair. – Peter Cordes Jul 25 '15 at 15:59
  • @Gold_Sky said *"I really did not know about fgetpos."* you **HAVE TO** know about `fgetpos` because `fsetpos` can only be used with values obtained from `fgetpos`. See this answer (which was not the accepted answer) http://stackoverflow.com/questions/3348637/fgetpos-fsetpos-and-ftell-fseek/21042679#21042679 – Weather Vane Jul 25 '15 at 18:25