mardi 7 avril 2020

In C++, How to safely create a file if it's not exist, but not to open it if it's exist?

If I directly using ios::out to open a file, then it will overwrite it or modify it if the file was existed.

But if I check if there is a file named "XXX" with ios::in, and create it with ios::out if it's not exist, the file "XXX" may be created by another program during this period, then the file "XXX" will be overwrote or be modified just like the first case.

How can I safely create a file with fstream in C++?

Aucun commentaire:

Enregistrer un commentaire