Results 1 to 7 of 7

Thread: Truncate bytes from start of file

  1. #1

    Truncate bytes from start of file

    Hi all.

    Well, i want to delete some bytes from start and at the end of untyped files.
    I dont have problems with deleteing it from end of file. For example i want to delete 5 bytes from EOF. This is my method:
    Code:
    seek(f,filesize(f));
    seek(f,filepos(f)-5);
    truncate(f)
    Works OK for me. Deletes 5 byte from end of file.
    But how to delete bytes from start of file? Because probably that "minus some bytes" does not work??
    I have to work with really big files (~700-1GB).
    All i need to is delete some bytes from beginning of file and leave other bytes like they are. The bytes i delete i will replace with my own bytes. But biggest problem is how to delete bytes from beginning of file so that other bytes are untouched?

    Any ideas, algos?

    thx in advance.

  2. #2

    Truncate bytes from start of file

    You could just copy the data you want to a new stream. Then just copy it back if needed.

  3. #3

    Truncate bytes from start of file

    thx 4 reply.

    Yeah, by looking answers to ppls questions who had same problem, this seems to be only solution
    But its big problem with such file sizes, about 700MB read into memory or temporary file and ...
    blah.
    Well, i will put this work and then i will think about, how to optimize it somehow. Im testing it with small files right now. To see how it works.

  4. #4

    Truncate bytes from start of file

    This is very easy case with TFileStream. When you write to it, it does not insert between but overwrite data. So if you have constant length block on start you can rewrite it anytime you want. Deleting data from the start would mean copying entire rest of file into a buffer and writing it to the start... and that is very highly unrecommended (/me imagines a 100mb file).

  5. #5
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25

    Truncate bytes from start of file

    Yeah, unfortuantely writting the whole file over cannot be avoided. It's the way the file system works. I guess you could think of each file as a type of FILO stack.
    Jason McMillen
    Pascal Game Development
    Co-Founder





  6. #6

    Truncate bytes from start of file

    I'm not 100% on this, but wouldn't truncing some bytes off the end of a file still require a rewrite of the whole file anyway? Is there a real difference in speed where/how ever you modify the data :?:

  7. #7

    Truncate bytes from start of file

    If you use Filestream, you can call
    FStream.Size:=NewSize;
    That manipulates file size very quickly and allows you to truncate or reserve more space to a file, however you don't have to reserve that space to write in file. You can move in a file with Position property and make precise write and read calls to wanted points that do not affect rest of the file.
    Small speed trick to this:
    -Never make a loop that reads/writes file (using Position or Seek) through from end to start as it is way slower than when doing from start to end. Also avoid random data...keep structure clear.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •