PDA

View Full Version : Truncate bytes from start of file



BytePtr
12-01-2006, 10:44 AM
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:

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.

K4Z
12-01-2006, 11:17 AM
You could just copy the data you want to a new stream. Then just copy it back if needed.

BytePtr
12-01-2006, 11:42 AM
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.

User137
12-01-2006, 01:09 PM
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).

WILL
13-01-2006, 01:43 AM
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.

K4Z
13-01-2006, 11:18 AM
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 :?:

User137
13-01-2006, 01:19 PM
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.