Making a file shorter

BlitzMax Forums/BlitzMax Beginners Area/Making a file shorter

Mordax_Praetorian(Posted 2006) [#1]
Well, after such a long time with no problems at all, here I am again begging for assistance

I am using a file (amoungst other things) for data storage

The sort of data it stores is not used often and is by no means suitable to be kept in RAM

The last part of the file consists of a string (not the variable type) of characters of varying length

At certain points (i.e. quitting the game), this entire string of characters nees to be removed from the end of the file

The file is already open (using OpenFile) during this "cleanup" to modify other characters in the file back to their original state, using SeekStream and WriteByte

Counting the number of characters on the end of the file is no problem, I can SeekStream to a marker "E:" which tells the engine where the string starts, however removing them is

After looking through the documentation, it would appear that BMax has absolutly no command that can remove characters from a file, without overwriting them with something else, thus meaning that the file can never get any shorter

Nor is there a command that simply shortens an open file, which I could use to shave off the right number of characters

I need a workaround thus to do the following:
-Remove a variable number of characters from the end of a file, or after the current "cursor" position in the file

This seems like a simple operation to me, and one that Blitz really should be capable of

This problem really couldnt have come at a worse time, as my new housemate is convinced that Blitz is very limited purely due to the fact that it isnt C++, and I am very eger to demonstrate that this is not the case


Beaker(Posted 2006) [#2]
This is a common problem in all languages (at least without database commands, which do the same as below but internally), and is not particular to Blitz.

Normally you would just write a new file (or overwrite the same one) with the characters removed.

No way around this.

If speed is an issue then you may need to be very selective about when you actually reconstruct the file. You can always write an Int at the beginning of the file that holds the eventual required length.

Hope this helps.


(tu) ENAY(Posted 2006) [#3]
Get a different housemate :)


Mordax_Praetorian(Posted 2006) [#4]
lol Enay, I'll consider it :p

Beaker: Ty very much

If that is the way it is done then that is the way I will do it

I only need to run the function when exiting the game, or on starting the game after a crash, and I suppose speed isnt so essential in either of those situations


kenshin(Posted 2006) [#5]
Wouldn't it be quicker if you use a seperate file for storing the string and then simply delete it on exit or crash?


Mordax_Praetorian(Posted 2006) [#6]
Kenshin, Y'know, you are completely and totaly right and I'm gonna do that, ty


klepto2(Posted 2006) [#7]
For deleting just a few bytes to the end i would suggest the following:

Load the desired File to a bank with LoadBank(url), then simply resize the bank to the size you will need e.g: ResizeBank(MyBank,BankSize(MyBank)-256) and at least save it back to the old place with SaveBank(url).