View Full Version : File, Get/Put...Erase??


JaedenRuiner
06-19-2007, 09:29 AM
Hello,

Given a file opened as such:

open sFileName for Binary Access Read Write As #FileNum

You can easily use the Seek, Get, and Put statements to access the binary data of the file. So you may have to improvise your own file format, but it works to achieve the simplistic end. However, the Put statement just writes the data to the file without any position checking. So:


Dim S as String
S = "Hello World"
Put #FileNum,,Len(S)
Put #FileNum,,S

Write a length integer and a string to the file, total size=13 bytes. But now, i decide to Seek #filenum,1 (to go to the start of the file) and say now I'm writing a smaller string:


S = "My World"
Seek #FileNum,1
Put #FileNum,, len(S)
Put #FileNum,,S


Total Size written: 10 bytes
But the file size is still 13 bytes from the previous write. There are 3 extraneous characters that aren't doing anything. Is there any way (built in) to truncate the file down to the current position, sort of a clear or purge that eliminates all data after a certain point and resets the filesize/eof?

Thanks

Jaeden "Sifo Dyas" al'Raec Ruiner

Moniker
06-19-2007, 03:50 PM
There is no built-in delete function. You'd have to manually maintain it, or use a Random file mode instead. Random allows you to move from record to record, which makes the navigation easier at least. When you're trying to make custom file types, you have to make markers between the records. For example:

~Record 1~This is Record 2~And So On~

You can use InStr to count through the tilde symbols (~) to get to a specific record, and then use a combination of Left, InStr, and Mid to remove, modify, etc. It's a fairly complex way to do things, though, and not really recommended given the other options available. I will say it's an easy way to encrypt, but that's a different story.