1. Hey! Guest! The 34th GMC Jam will take place between August 22nd, 12:00 UTC (Thursday noon) and August 26th, 12:00 UTC (Monday noon). Why not join in! Click here to find out more!
    Dismiss Notice

GM:S 1.4 [SOLVED] Clearing files instead of deleting and recreating them

Discussion in 'Programming' started by marasovec, Aug 12, 2019.

  1. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    309
    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: Aug 12, 2019
  2. Bart

    Bart Member

    Joined:
    Jun 21, 2016
    Posts:
    144
    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.
     
    marasovec likes this.
  3. marasovec

    marasovec Member

    Joined:
    Sep 15, 2016
    Posts:
    309
    Oh boy it works. I've never thought about using buffers this way. Thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice