Search code examples
c++macostilde

Tilde ~ makes trouble when using fstream on Mac


I do not exactly know whether I have an IDE/toolchain question, a Mac question or a C++ question:

I want to save a file with ofstream. That works as expected. But when I save to a filepath with a tilde like "~/Desktop/test.txt", ofstream::good() reports an error. However, I can successfully use a path like "/Users/Michael/Desktop/test.txt".

This holds for the Xcode debugger, for a run with Eclipse, as well as for a direct call of the exectuable from the console.

What is the problem with the tilde "~" here?


Solution

  • There is no path ~, that's something that's interpreted by your shell. You will need to expand it to whatever the environment variable HOME is set to.

    If you're calling your tool via the shell this will be expanded for you in advance. If you're within Xcode, you'll need to specify $HOME explicitly.