2

I need to call fsync() to secure my data. Can you please help me to get file descriptor from ofstream.

string tmpconfig("config.tmp");
ofstream tcfg(tmpconfig.c_str());
if(tcfg)
{
tcfgNV.compactPrint(tcfg);
tcfg.flush();
if(!tcfg.good())
{
    tcfg.close();
    ::remove(tmpconfig.c_str());
    cout << "Failed to write the main configuration file: " << topconfig << endl;
    return false;
}
REPORT_INFO("Syncing the file: " << topconfig);
// TODO I need to call fsync() here to secure my data.
//tcfg.pubsync();
tcfg.close();
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    There's no portable way to get a native handle to the underlying file. There might be non-standard extension of the implementation, but without knowing which compiler and standard library implementation you use it's impossible to say. – Some programmer dude Oct 31 '17 at 11:21
  • Also, the "need" to use `fsync` is often a red herring, and the real problem is something else with other portable solutions. – Some programmer dude Oct 31 '17 at 11:22
  • The stdlib exists in many cases to abstract away platform-dependent hackery, so of course the Standard file stream doesn't expose that implementation detail. – underscore_d Oct 31 '17 at 11:26
  • 1
    Possible duplicate of [Getting a FILE\* from a std::fstream](https://stackoverflow.com/questions/109449/getting-a-file-from-a-stdfstream) – underscore_d Oct 31 '17 at 11:26
  • Is it ok to use; tcfg.rdbuf()->pubsync(); – Ashutosh Malik Nov 01 '17 at 09:36

0 Answers0