Saturday, 15 September 2012

c++ - Clearing the content of file at startup and then append -



c++ - Clearing the content of file at startup and then append -

i want append text in file std::fstream. wrote this

class foo() { foo() {} void print() { std::fstream fout ("/media/c/tables.txt", std::fstream::app| std::fstream::out); // fout } };

problem construction every time run program, texts appended previous run. illustration @ end of first run size of file 60kb. @ origin of sec run, texts appended 60kb file.

to prepare want initialize fstream in constructor , open in append mode. this

class foo() { std::fstream fout; foo() { fout.open("/media/c/tables.txt", std::fstream::out); } void print() { fout.open("/media/c/tables.txt", std::fstream::app); // fout } };

problem code 0 size file @ during execution , @ end of run!!

you need open file 1 time :

class foo() { std::fstream fout; foo() { fout.open("/media/c/tables.txt", std::fstream::out); } void print() { //write whatever want file } ~foo(){ fout.close() } };

c++ append fstream

No comments:

Post a Comment