mercredi 13 février 2019

Alternative to bash sync in C++

I am writing file handling code using C++. Problem is after writing to file if power off occures immediately, file is not getting written.

As I found problem is due to delay in writing from system buffer to persistent file storage.

I over came this scenario with bash sync command. here is code snippet

cout << "Writting to file" << endl;
ofstream fout("demo.dat", ios::out);
fout << "hello world" <<flush;
fout.close();
system("sync");
cout << "file written" << endl << "Sleeping for 3 secs"<<endl;
this_thread::sleep_for(chrono::seconds(3)); //disconnect power here
... some more statements

Problem is system() is considered bad keeping performance in mind, as I have to write to files very frequently.

Please let me know if C++ provides any better way.

I tried with std::flush, pubsync() but it does not work.

I am doing C++ style file handling so can not use C style file handling.

Aucun commentaire:

Enregistrer un commentaire