Cut binary file

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
nyfiken
Newbie
Posts: 7
Joined: Sun Aug 26, 2007 11:57 pm

Cut binary file

Post by nyfiken » Tue Dec 04, 2007 3:04 pm

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
Last edited by nyfiken on Tue Feb 15, 2011 10:32 pm, edited 1 time in total.

User avatar
Codemss
Veteran
Posts: 124
Joined: Sun Jun 24, 2007 6:49 am
Location: Utrecht, The Netherlands
Contact:

Post by Codemss » Tue Dec 04, 2007 3:18 pm

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...
Check out my site: <a href="http://members.lycos.nl/rubynl">Click here</a>
Hope you like it. Send some feedback if you want: <a href="mailto:basicallybest@live.nl">Mail me</a>
Codemss, before known as RubyNL

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Re: Cut binary file

Post by Mac » Tue Dec 04, 2007 4:29 pm

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

Nemesis
Coder
Posts: 36
Joined: Mon Aug 16, 2004 12:54 pm
Location: Colorado Springs, Colorado

Post by Nemesis » Tue Dec 04, 2007 5:37 pm

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

Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Shortening a file

Post by Mac » Tue Dec 04, 2007 9:08 pm

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

User avatar
burger2227
Veteran
Posts: 2446
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 » Wed Dec 05, 2007 3:29 am

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
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0

Post Reply