Page 1 of 1

Cut binary file

Posted: Tue Dec 04, 2007 3:04 pm
by nyfiken
If I want to shorten a binary file, how can I do that?

When i use LOF(X) QB says the file has n length. Is it possible to trick QB so LOF(X) says n-1 length, or do I have to make a new shorter copy of the file?
________
Oregon Medical Marijuana Dispensaries

Posted: Tue Dec 04, 2007 3:18 pm
by Codemss
You mean you want to compress the file? :S
BTW, all the files can be opened with OPEN file$ FOR BINARY AS #1, it's actually not that they "are" binary files. They are just changed the binary way. I don't know about LOF(X) but I think it just returns the amount of bytes that that file uses.
Ow, wait... You mean you want to discard some bytes at the end of a file? Shit I don't know how to do that. I only know that you can change the bytes with PUT and GET. Maybe if you PUT a 0 at the end. I'm not sure though...

Re: Cut binary file

Posted: Tue Dec 04, 2007 4:29 pm
by Mac
nyfiken wrote:If I want to shorten a binary file, how can I do that?
Good question. You would think this would be easy, but I don't know any way.

If the whole file fits in memory, you could read it in, close, open for output, close, and the write it out again.

Why bother, though, to shorten a file? What is your application? How did you wind up with spare space at the end?

I can imagine one doing this. I have records which have a deleted flag:
0 xxxx
0 xxxx
1 xxxx
0 xxxx
0 xxxx
1 xxxx
1 xxxx
etc.

I want to compress the file so all the undeleted records are at the front.

Easy

But now I want to get rid of the space at the end. My answer would be to not depend on LOF for the number of records, but instead, put some kind of header record at the front of the file. Now you don't mind the space at the end. You will use it someday when you add new records.

Mac

Posted: Tue Dec 04, 2007 5:37 pm
by Nemesis
There are many ways you can shorten a file, here's one...

Open the file as binary, read the data, (either byte X byte or in chunks), manipulate the data, (shorten it, flip it, mangle it, do whatever you want),
write the data back to your file.

Cya,

Nemesis

Shortening a file

Posted: Tue Dec 04, 2007 9:08 pm
by Mac
Yes, as I already said "If the whole file fits in memory, you could read it in, close, open for output, close, and the write it out again."

But if the file won't fit, you have to create a new file.

If your one file is so big that there is no room for two on your disk, you are doomed.

So I suggested just leaving the file big and keep your own pointer (not LOF) to know where there is space to write new records to.

Mac

Posted: Wed Dec 05, 2007 3:29 am
by burger2227
Makes sense! The record fields can be marked and overwritten in that case. To shorten the file is most likely a futile overature. You may have to add something to the file later!

I hope you are NOT thinking like Bill Gates and the 640K limit in his garage. Windows hogs memory and the hard drive, so keep the space in your Monster sized file............ It will never be that bloated!

Ted