Thread: how to read a binary file backwards?

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    269

    how to read a binary file backwards?

    I am writing a file in binary using fwrite. I am opening this file for read and write. After each write, the file pointer points to the end of the file, and I need to be able to read backwards x number of bytes.

    I could move the pointer backwards X bytes, and then do a forward read (I would prefer this way), but I'm not sure how to do this either.

    Any suggestions?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Look into fseek. It has a way to reference from the beginning of the file, the end of the file or the current position. One of the latter two methods should work for you.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Check out fseek(3): reposition stream - Linux man page. Example: fseek(fp, sizeof(record), SEEK_CUR);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    269
    fseek seems to be exactly what I'm looking for! Thanks so much, again!!

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    269
    With fseek, there's no way to go backwards from the end is there?

    For example, the current pointer is at the end, and I know I need the last 4 bytes back. Can i just go back 4 bytes?

    It seems that I have to start from the very start and go forward total_bytes - 4bytes?

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    269
    maybe I have to a combination of fgetpos and fsetpos

  7. #7
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    If forward is positive, what would backwards be?

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    269
    Some times, I'm smart. Others, the -1 of smart.

    It works. Thanks you lol.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by dayalsoap View Post
    With fseek, there's no way to go backwards from the end is there?

    For example, the current pointer is at the end, and I know I need the last 4 bytes back. Can i just go back 4 bytes?

    It seems that I have to start from the very start and go forward total_bytes - 4bytes?
    Read the docs... There are in fact three options for seeking in a file... SEEK_SET relative to the beginning of the file, SEEK_CUR relative to the current position and SEEK_END relative to the end of the file...

    Reading is good....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. efficient binary file read
    By Marv in forum C Programming
    Replies: 15
    Last Post: 11-19-2007, 04:42 PM
  2. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM
  3. Read in binary file ( pointers )
    By Giant in forum C Programming
    Replies: 41
    Last Post: 06-23-2005, 04:54 AM
  4. Read a binary file
    By Sue Paterniti in forum C Programming
    Replies: 8
    Last Post: 04-29-2002, 02:36 AM