3

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?

Community
  • 1
  • 1
Vincent
  • 57,703
  • 61
  • 205
  • 388

1 Answers1

2

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451