Thread: fgetpos fsetpos problem/confusion

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    14

    fgetpos fsetpos problem/confusion

    I'm having a problem with fgetpos and fsetpos when i call fsetpos twice.

    Code:
    while(0 != fgets(buffer,100,input) && strtod(buffer,NULL) < LOWER)
    	fgetpos(input,&start);
    	
    fsetpos(input,&start);
    	
    while(0 != fgets(buffer,100,input) && strtod(buffer,NULL) <= upper)
    	lines_needed++;
    	
    rewind(input);
    fsetpos(input,&start_copy);
    the code above works correctly, however when the rewind(input); line is removed, the second fsetpos fails to do anything at all.

    At first i thoght that maybe fsetpos can only work forwards in the file, but the first fsetpos in fact works backwards, so the function must work both ways

    Any ideas as to why this is happening?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Check the return value of fsetpos(). If it returns error, check errno.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    fsetpos returns 0 for both calls, with or without the rewind line
    Last edited by Skeksis; 12-06-2010 at 02:52 PM.

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    BTW, i've also tried making a copy of start and using it in the second call just to check whether fsetpos was changing anything there, no difference.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess you should make sure that the relationship between "start" and "start_copy" is what we would expect from the names, as the "easy" diagnosis from what we see is that you set start_copy to the end of where you were just reading instead of the same as start.

    EDIT: This appears to have crossed your other message. Can you give us something compilable? My tests here are working fine.
    Last edited by tabstop; 12-06-2010 at 03:03 PM.

  6. #6
    Registered User
    Join Date
    Dec 2010
    Posts
    14
    sorry to have bothered you guys, the problem appears to have disappeared. Must have had something minor wrong that got fixed while messing around with it debugging.

    There was also the secondary problem that if LOWER == 0 then fgetpos would never run, now fixed by using a do while loop instead of while.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Does fgetpos work after reopening a file?
    By johny145 in forum C Programming
    Replies: 5
    Last Post: 05-12-2005, 02:09 PM