file_open_write appends to file content

Posted By: TehV

file_open_write appends to file content - 03/01/14 14:52

Hi,

I have a function that writes contents to an XML file. I appear to have hit a roadblock since the program appends to the XML file instead of overwriting the existing content.

This is the relevant code:
Code:
function writeXML() {
	var file = file_open_write("out_info.xml");
	STRING* str = "";
	
	str_cat(str,"<out_info>\n\t");
	str_cat_num(str,"<tasks_total>%.0f</tasks_total>\n\t",totaltasks);
	str_cat_num(str,"<current_task>%.0f</current_task>\n\t",current_task);
	str_cat_num(str,"<frame_rate>%.0f</frame_rate>\n\t",fps_snap);
	str_cat_num(str,"<do_mode>%.0f</do_mode>\n",runmode);
	str_cat(str,"</out_info>");
	
	file_str_write(file,str);
	
	file_close(file);
}



Anyone have any ideas?
Posted By: sivan

Re: file_open_write appends to file content - 03/01/14 16:37

strange, I use something similar to overwrite existing text files and works... except that I would use str_cpy instead of the 1st str_cat.
Posted By: Uhrwerk

Re: file_open_write appends to file content - 03/01/14 18:53

Have you even tried the code you posted? It works perfectly as supposed..?
Code:
#include <acknex.h>

function writeXML()
{
	var file = file_open_write("out_info.xml");
	STRING* str = "";
	
	str_cat(str,"<out_info>\n\t");
	str_cat(str,"</out_info>");
	
	file_str_write(file,str);
	
	file_close(file);
}

void main()
{
	writeXML();	
}

Posted By: Superku

Re: file_open_write appends to file content - 03/01/14 22:48

I have a related question:

Code:
void do_foo
{
	STRING* str = "";
	
	modify str;
}


Is this legit? Shouldn't it be a str_created STRING object? If not, when is str_create necessary (apart from situations where I really want to create and optionally return a string)?
Posted By: Uhrwerk

Re: file_open_write appends to file content - 03/01/14 23:42

I guess that is his original fault. It is legit AFAIK but initializes str with the same string "constant" every time. Now as we all know there is no such thing as real constants in LiteC so every time this function is called the same content is added over and over again. That is most likely the reason why he thinks the data is appended to the file while in fact he just appends to the string every time he calls this function and that causes the same stuff to appear multiple times in the file.

str_create is necessary when you want to have a fresh string every time you call that function.
© 2024 lite-C Forums