2

I flush the ofstream after writing to it, and need to update to the actual file and launcher an external program to open it, and I need to keep the ofstream open without closing it. I did:

ofstream f("..", std::ofstream::out | std::ofstream::app | std::ofstream::trunc);
f << data << std::endl;
f.flush();

But the file does not even exists when the external program executes. The same problem here. How can I block the program till the file actually exists in the file system?

Community
  • 1
  • 1
SwiftMango
  • 15,092
  • 13
  • 71
  • 136

2 Answers2

2

If that is your code then ".." is not a valid filename in the declaration of ofstream f(). It's normal that no file is created. Flush should be enough for file usage. Append and Truncate together has not much sense. Maybe your file can't be accessed (sharing exclusive) but was still created.

andreaplanet
  • 731
  • 5
  • 14
1

From your application's point of view, the file should exist once you open it for writing. Validate this by performing the following steps:

1.) Open the same file as std::ifstream. The call should succeed. (Except in rare cases when your std::ofstream creates a mandantory lock on the file.)

2.) Change std::ofstream to std::fstream. Try to read from the file after the f.flush(). You should be able to retrieve the value of data. If not, then you probably have messed up you memory somewhere in your application.

3.) Make sure that no other process on the system is accessing the same file, e.g. deleting it as soon as it comes to life.

Note: The OS or the filesystem driver need some time to actually make any file changes available to other processes. This delay can be particularly long on slow storage or when the system load is high. You do not have control over this delay. In such a case, you should look into inter-process syncronization.

ManuelAtWork
  • 2,198
  • 29
  • 34