Legacy GM [SOLVED] Clearing files instead of deleting and recreating them

marasovec

Member
I made a simple encrypting script because only base64 is not enough and to make it work I need to rewrite the used file. I can't find a way to simply delete the text inside so I have to delete and recreate the whole file. But doing this doesn't seem much effective

Code:
if file_exists(filename)
    {
    save_file_decode(filename);
    load = ds_map_secure_load(filename);
    save_file_encode(filename);
    }
save_file_encode script
Code:
var str = "";
var newstr = "";

// read
var file = file_text_open_read(argument0);
str = file_text_read_string(file);
file_text_close(file);

// do some crypting magic

// write
file_delete(argument0);
var file = file_text_open_write(argument0);
file_text_write_string(file, newstr);
file_text_close(file);
 
Last edited:

Bart

WiseBart
You can use buffers instead.

Use buffer_load and buffer_save instead of file_text_open_read/file_text_open_write.
Instead of file_text_read_string and file_text_write_string you can use buffer_read and buffer_write, respectively, with a buffer_text type.
To get the equivalent of that file_delete you simply need to seek to the start of the buffer to overwrite its existing contents.

For a string that doesn't change length the code looks something like this:
Code:
var str = "";
var newstr = "";

var buffer = buffer_load(filename);
str = buffer_read(buffer, buffer_text);

// Perform the necessary stuff here
// The result ends up in newstr

buffer_seek(buffer, buffer_seek_start, 0); // Jump back to the start (i.e. overwrite from the beginning / do "file_delete")
buffer_write(buffer, buffer_text, newstr);
buffer_save(buffer, filename);
buffer_delete(buffer); // Clean up
One thing you need to add to the above code is to check the size of the buffer and make sure it is adjusted according to the length of the string you write back to it.
 
Top