Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combination of std::ios::openmode to truncate if the file exists but prevent new file creation?

I have asked earlier if there was a combination of openmode to avoid modifications of an existing file. Now I would like to know if the contrary is possible :

  1. if the file already exists, truncate it (std::ios::trunc)
  2. if the file does not already exist, do nothing

Is there an std::ios::openmode available to do that with an std::ofstream?

like image 227
Vincent Avatar asked Jan 22 '26 22:01

Vincent


1 Answers

To the best of my knowledge, there is no such an open mode (it does not look like a very common use case). I guess you could first check whether the file exists and, only if it does, open it with trunc open mode.

like image 98
Andy Prowl Avatar answered Jan 25 '26 16:01

Andy Prowl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!