How do I (fseek, ftell, stat)?

Discussion in 'C Programming' started by cedarson@gmail.com, Feb 14, 2006.

  1. Guest

    I am writing a program and have been instructeed to use the 'fseek',
    'ftell', and 'stat' functions, however, after looking in the online
    manual for each of these, I am still unsure on how to use them. In my
    program, I am to write a code that opens a file, uses 'stat' to
    determine the file size, use 'fseek' to move the offset of the pointer,
    and finally use 'ftell' to obtain the file pointer index. Will someone
    please help? Again, thanks for your help on my other post all who
    replied. You help is greatly appreciated.
     
    , Feb 14, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > I am writing a program and have been instructeed to use the 'fseek',
    > 'ftell', and 'stat' functions, however, after looking in the online
    > manual for each of these, I am still unsure on how to use them. In my
    > program, I am to write a code that opens a file, uses 'stat' to
    > determine the file size, use 'fseek' to move the offset of the pointer,
    > and finally use 'ftell' to obtain the file pointer index. Will someone
    > please help? Again, thanks for your help on my other post all who
    > replied. You help is greatly appreciated.


    Some files can only be processed from the beginning
    to the end: when you are reading from a keyboard or writing
    to a socket, the only datum you can read or write is the
    "next" byte. This is "sequential access."

    Some files may permit you to jump around: read some
    data near the beginning, then leap to a spot near the end
    and read some more, then reposition to someplace in the
    middle and overwrite what was there before. This is
    "random access." fseek() is the repositioner: without it
    the next read or write operation begins at the byte after
    the last one read or written, but fseek() changes the
    "current position" to some other arbitrary spot in the file
    so you can read or write starting at that spot instead.

    ftell() tells you where you are: If you've read or
    written some amount of data from some starting position,
    ftell() reports the position at which the next read or write
    would begin, if you were to perform one. It's useful when
    you want to "bookmark" a spot in the file, when you want to
    remember it as a place you might want to return to.

    There's another pair of functions that can do a similar
    thing: fgetpos() reports the current position within a file,
    and fsetpos() returns to a reported position. Conceptually
    they are like ftell() and fseek(), but the interface allows
    for a more flexible implementation. For example, ftell() and
    fseek() use a `long' value to describe a position within a
    file; on systems where files can be larger than LONG_MAX
    characters, they are impotent but fgetpos() and fsetpos()
    will work anyhow.

    Finally, stat(): It is not a C function, but is specified
    by POSIX and perhaps other related standards. Supposedly it
    reports the "size" of a file -- but a file's "size" can be a
    slippery notion. For example, on Windows a file containing
    the two lines "Hello,\nworld!\n" most likely is sixteen, not
    fourteen, bytes long: Windows ordinarily inserts an invisible
    '\r' character before each '\n' written to a file. On POSIX
    systems such things don't happen, but C runs on systems that
    are outside the bounds of POSIX, so you cannot count on stat()
    to deliver an accurate notion of a file's "size." (If you're
    not on a POSIX system, you can't even count on stat() being
    present!)

    You've got a good deal more reading to do; I hope the
    above provides a framework that helps you understand what
    you must read.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Feb 14, 2006
    #2
    1. Advertising

  3. Guest

    Thanks, Eric. You explain it thoroughly.
     
    , Feb 14, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steven T. Hatton
    Replies:
    3
    Views:
    9,760
    Pete Becker
    Jun 2, 2005
  2. Leslaw Bieniasz

    How to speed up ftell()/fseek()

    Leslaw Bieniasz, Jun 6, 2005, in forum: C++
    Replies:
    7
    Views:
    4,970
    Lionel B
    Jun 8, 2005
  3. Kenneth Brody

    Text mode fseek/ftell

    Kenneth Brody, Mar 31, 2006, in forum: C Programming
    Replies:
    10
    Views:
    1,122
    Ben Bacarisse
    Apr 2, 2006
  4. Chen ShuSheng
    Replies:
    3
    Views:
    543
    Ancient_Hacker
    Aug 24, 2006
  5. Chen ShuSheng
    Replies:
    21
    Views:
    936
    jmcgill
    Aug 28, 2006
Loading...

Share This Page