Write in a file then remove it.

BlitzMax Forums/BlitzMax Programming/Write in a file then remove it.

Azaratur(Posted 2008) [#1]
I need to know how can i add something in a file and then remove the same thing.
I the way to add but i don't find a way to remove..

Aza


Brucey(Posted 2008) [#2]
Remove the file or remove the thing you added *from* the file?


Azaratur(Posted 2008) [#3]
remove the thing you added *from* the file
Aza


ImaginaryHuman(Posted 2008) [#4]
I guess you need to open the file for reading and writing and then seek within the file stream to the position you want and write or overwrite the data. What are you thinking of as `removing`, just writing blank data, or completely `cutting` a portion of the file out and joining the remaining parts together?


plash(Posted 2008) [#5]
You would have to know the position and length of the data you want to remove. Just load in the bytes until you hit the beginning position of the data you want to remove, then seek to the position + the length and continue reading data in. The data you loaded will be the data without the part you don't want, so just save it over the original.

EDIT: Actually instead of reading individual bytes, you could use stream.ReadBytes and use the beginning position as the size, then skip over the part you don't want, then calculate how much data is left after the end of the part you just cut out and read it in too.


markcw(Posted 2008) [#6]
Load the file to memory, find the bit you don't want, and write the bits you still want back to the file.


Azaratur(Posted 2008) [#7]
This is what i wanna do:

I wanna open a file (a mesh file), i don't really know what is inside of it..
I wanna add some random things (doesn't matter), in this case the file will be unopenable and when i need it i want just remove this part of file..

Is it possible? and how?

Aza


plash(Posted 2008) [#8]
Quite silly. If your trying to make the file unreadable/unusable by the people obtaining your program that is hardly a secure way to do it.
I would suggest encryption, there are plenty algorithms around.

EDIT:
Is it possible? and how?
Yes..

Seek to a positon in the file.
Write a bunch of nonsense and close the file.

When loading:
Simply way would be to seek over the data, as mentioned before, then save a temporary file and load it back in with your mesh loader, maintaining the buggered file format of the original.


Perturbatio(Posted 2008) [#9]
As long as you write the data to a known area in the file, then all you need to do is read in the file data via a stream and spit it back out to a temporary file then overwrite when done. Just skip the data you don't want when writing out.


Azaratur(Posted 2008) [#10]
I can't use encryption for the moment..
Aza


TaskMaster(Posted 2008) [#11]
The only way you can write to a file that already exists is either to append to the end, or overwrite something elsewhere. You cannot insert data somewhere in a file.

If you need to insert some data, you have to create a new file. Write some of the old data, write your new data, write the rest of the old data. Then delete the old file and rename the new file.

If you want, you can read the entire file into memory, then rewrite the whole file from scratch writing your new data where you need it. This way you do not have to use the temporary file method.