Thread: How to set end of file??

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    How to set end of file??

    Helllo,

    I just started file part in C++. I can edit record in file but when i delete problem's coming. I dont know how to set end of file in file. I mean say there are 5 records in employee file

    Now i ask which employee name you want to delete . Now suppose that record is 2nd one so what is do is copy 3rd record to 2nd one 4th to 3rd one and 5th to 4th one. But the problem is that last record appears twice. I want to set end of file after 1st appearance of last record. Please tell me how can i do it?

    And also if anybody has some other good way to delete records then kindly tell me that also.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    The algorithm usually goes something like:
    1. Open the record file.
    2. Open the new file, or if it is the same, a temp file.
    3. Read the records.
    4. Write records that you want to keep to the new file.
    5. Close and remove the old file.
    5b. If necessary, rename the temp file to the old name.

    Then you're done.

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    Code:
    fseek(fileptr, 0L, SEEK_END);
    where fileptr is the open file pointer.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    That won't destroy old records.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Yes that algorithm i know it. But if i want to perform it in the way that i mentioned above then how to do it?

    How can i write eof to the file?

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    EOF is just a stream signal for the end of the file, it has nothing to do with deleting a record from a file. If you want to do that, you need an algorithm like the one I talked about.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Any other suggestions?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by chottachatri View Post
    Yes that algorithm i know it. But if i want to perform it in the way that i mentioned above then how to do it?

    How can i write eof to the file?
    There is no standard way. However, most operating systems provide a "truncate" function which is used to reduce the size of a file. On POSIX systems this function is truncate() or ftruncate(). On Windows it is SetEndOfFile().

    Copying to a new file and then renaming is the only standard way to do it, but terribly inefficient.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    That's why DBs don't really remove records from tables but only mark them as deleted... And reuse them on adding new records...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As vart hints, another way to solve the problem is to have a way to "remember" deleted records, rather than actually remove them from the file. This can be done in several ways.

    Some possible options:
    1. A linked list of deleted records.
    Have a "header" record at the start of the file, which contains the first deleted record. In each of the next deleted records, store the NEXT deleted record. When deleting something, just link that into the header record and point the newly deleted record to what was previously in the header.
    2. Have every record hold two fields to know if it's a deleted record and where the next deleted record is. [Could be made into ONE record if -1 is used to indicate "not deleted".
    3. Another variant is to shuffle all the data to a "earlier" record, and keep all deleted records at the end. You still need to know how many or which records are deleted, but it may make the logic a bit simpler to store all deleted records at the end of the file.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. disposing error
    By dropper166 in forum C# Programming
    Replies: 2
    Last Post: 03-30-2009, 11:53 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM