Hi Yasha,
you have to close the file after reading/writing.
in your code the function pointer of file_open_write() should be stored in a variable for later accessing it to close the file:
var filehandle = file_open_write(str_create(profile_doc)); //opens your file and store if succed the handler into filehandel.
...
file_close(filehandle); // closes the file after write executions