Thread: getting last element in file

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    118

    getting last element in file

    Hi, i am trying to get the last structure in a binary file and am using
    fseek(t,sizeof(struct man),SEEK_END) but its not working. I have tried substituting SEEK_END - sizeof(struct man) .
    fseek(t,sizeof(struct man),SEEK_END-sizeof(struct man)) still didnt work

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You don't want to read the data? To me, "get" equals "read". So I'm confused.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    He first has to seek to the appropriate position within the file prior to the read operation. It's been a long time since I've used fseek but wouldn't it be something like:
    Code:
    fseek(t,-sizeof(struct man),SEEK_END);
    I think that would go sizeof(struct man) bytes backwards (towards the start of the file) from the end of the file... which is what you'd want. You'd then be able to do an fread call.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector remove element of element
    By Ducky in forum C++ Programming
    Replies: 6
    Last Post: 09-12-2010, 03:24 PM
  2. STL set max element in O(lgN)
    By Klesk in forum C++ Programming
    Replies: 4
    Last Post: 04-24-2008, 03:41 PM
  3. first element: 0 or 1?
    By sh3rpa in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2007, 10:11 PM
  4. element ?
    By matthughes in forum C Programming
    Replies: 7
    Last Post: 05-18-2007, 12:45 AM
  5. list first element
    By wyrwidab in forum C++ Programming
    Replies: 1
    Last Post: 10-24-2005, 11:42 AM

Tags for this Thread