lundi 20 mai 2019

C++ How efficiency update content of file

I was wondering how can I update values in file ? Suppose I have something like this:

 //thread 1:    
       std::vector<int> v;

 //v is changed
       ...

 //thread 2:   
 //apart from synchronization

       std::fstream file;
       file.open("test.txt", std::ios_base::out);
       while(true)
       {
           //file.seekp(0, std::ios_base::beg); -> with this almost works, but update takes too long
           for (auto const& e : v)
               file << e << ',';
           file.flush();
       }

Due to the performance efficiency open and close file in every iteration is non-starter, so I want to open the file once and then only updates values in file, but after first iteration file is not updated.

Aucun commentaire:

Enregistrer un commentaire