Is there an EraseByte() like function?

Blitz3D Forums/Blitz3D Beginners Area/Is there an EraseByte() like function?

Rob the Great(Posted 2011) [#1]
I'm playing around with save files right now, and I'm within inches of having some really usable code for myself. BUT, I'm stuck with a problem where I can't erase data from an already created save file. I can modify it with no problems, but what tends to happen is that my save file continues to get larger and larger with empty space at the end.

What's the best way around this problem? I've thought of using banks, but I'm still not sure that it's going to fix anything.


MCP(Posted 2011) [#2]
It depends how big the file is Rob. If it's a small file i would just load the whole thing into a memory bank, make the necessary changes, then save the whole thing back making sure the original is overwritten with the WriteFile command. (I think you use the WriteFile command. It's been a while since I've used Blitz3D.)

Hope that helps.


Rob the Great(Posted 2011) [#3]
Ah, so you can replace the file with WriteFile...Got it. That's all I needed.


Zethrax(Posted 2011) [#4]
Take a look at the SeekFile and FilePos commands too. In combination with the file writing commands they allow you to edit parts of a file without replacing the whole file. This can be useful if the file is some sort of large database.

In most cases you're going to want to just replace the entire file though, as MCP mentioned above

http://www.blitzbasic.com/b3ddocs/command.php?name=SeekFile&ref=2d_cat
http://www.blitzbasic.com/b3ddocs/command.php?name=FilePos&ref=2d_cat


Rob the Great(Posted 2011) [#5]
Yeah, I've been using both of those commands like crazy. Actually, my save file system is done now, but I'm curious if there's a work-around to a problem I came across while designing it.

The FilPos() and SeekPos() are great and do exactly what I need them to do, but there's one problem I had with them: If I write data that is less bytes than the original data, the original data still remains in the file to the end of the line. I'm using the WriteLine() command for my data, so it's more like an INI file than anything, and I'm breaking it into sections so that I can write save information to a single section and then read back data from that specific section (again, much like an INI file, but specific to my own purposes).

Long story short, replacing the string with a shorter string leaves the end of the longer string behind. Thus, I was wondering if there was a function to erase the remaining data on that line. A backspace, so to speak.

But, I came up with a system using types that was able to resolve this problem. I just didn't realize that you could use WriteFile() to replace a file already made, so it's all good.


Matty(Posted 2011) [#6]
In my experience it is easiest to simply read the whole file into a bank, make changes then write it all out again. Simply use "readbytes" and "writebytes" to do so.


Wings(Posted 2011) [#7]
Aggre with matty. to erase a byte you need to load the whole file into a bank, make a new bank copy new data to new bank. and last save it to file again as new file.


Yasha(Posted 2011) [#8]
it's more like an INI file than anything


*cough*

http://www.blitzbasic.com/codearcs/codearcs.php?code=2863

Load and save data from/to standard-format INI files. Accessor functions let you add fields and whole sections to the loaded structure without needing to think about the individual bytes of the file. Just hit INI_Write when done.


Rob the Great(Posted 2011) [#9]
@Yasha,
I actually looked at your INI creator before posting this topic. It works wonders, and it also gave me some great ideas on how to approach my system. However, whereas this is something I've never done before, rather than copy-paste someone else's code, I prefer to write my own. People often ask, "why re-invent the wheel", and my response has always been that nothing can help you understand the full complexity of the wheel more than designing it yourself.