Thread: question about fseek

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    60

    question about fseek

    Hey,
    Im trying to use fseek with a value input from stdin.

    int fseek( FILE *fp, long offset, int origin)

    Code:
    long start;
    .
    .
    .
    fseek(finp, start, SEEK_SET);
    while(c != '0') {
            c = fgetc(finp);
            printf("%d ", c);
    }
    So if start is say, 35, i want the printing of the file to start at position 35 from the beginning of the file. But istead it just starts from the beginning.
    I replace variable start with values such as "5L , 7L , etc" to check fseek's validity and it works fine. I just dont know how to pass it start so it works.

  2. #2
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    If the file size is less than 35 bytes than you can't put anything at offset 35.
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So are you sure start is 35?

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by tabstop View Post
    So are you sure start is 35?
    Hey, I am using
    Code:
    int main(int argc, char *argv[])
    so I had to convert the needed string to a long with function atol. duh..
    Thanks for the help ;0

    gp

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    What if I type in a negative number into your command line?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    60
    Quote Originally Posted by master5001 View Post
    What if I type in a negative number into your command line?
    I have some error handling before the values are processed


    Code:
    if((start < 0 || end < 0 || start > counter || end > counter || start > end)) {
    	fprintf(stderr, "Invalid range specified.\n");
    	exit(1);
    }

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Good man

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fseek failing - Error: Invalid argument
    By avi2886 in forum C Programming
    Replies: 14
    Last Post: 05-14-2009, 08:49 PM
  2. fseek() changing an unrelated variable?
    By aaronvegh in forum C Programming
    Replies: 3
    Last Post: 11-21-2007, 02:30 PM
  3. fseek high cpu usage?
    By chunlee in forum C Programming
    Replies: 2
    Last Post: 02-19-2005, 07:27 AM
  4. popen and fseek
    By mach5 in forum C Programming
    Replies: 4
    Last Post: 11-29-2003, 02:03 AM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM