How to check if file is in use?
You can assume that lsof ships on all macOS and you can also check the existence of that command before you call it, logging a nice friendly error message and or log message as you see fit if you want to code defensively.

You could also ask on a code-focused site what API exist to check for file existence if you want to go into specific C++ code (which would be off topic here), but from your question assuming you can call any command line tool, your overall design is fine to execute in a C++ program on macOS.