Ofstream MacOS gui app does not work question


There is something i don’t get regarding standard C++ file streams on MacOS. Here’s some very basic standard C++ code that creates a file and writes something into it:


std::ofstream myfile;
myfile.open (“testing123.txt”);
myfile << “Hello file system.\n”;

Now when i create a Console Application in the Producer and put this code in it works like a charm. However when i create a GUI Application in the Producer and put this code in, i don’t get an error but it just does not create a file…

I know that the JUCE file class works like a charm and i should use that instead ;-)hing Mac
I also suspect that this is not a JUCE issue but something MacOS related or so…

I just would like to understand what the problem is witch is why can i use std::ofstream in a Console app but not in a GUI app on MacOS… By the way on windows it works perfectly fine…


PS: the inculdes are iostream and fstream but for some reason not displayed here…

Do you really specify the relative file path that you posted above? If so, the file might actually be written but somewhere you would‘t expect it. To which location such a file is relative is dependent of from where the executable has been invoked etc. so it‘s likely that a GUI app contained in an App Bundle behaves different to a command line application.
You should always use absolute paths and as you already mentioned, the File class can help you with that.

Thank you so much !!! I had no idea that one should always use absolute paths in a MacOS application… But now i do :wink:

What i still find a bit strange though is why:
This does work: myfile.open ("/Users/martijn/test123.txt");
But this does not: myfile.open ("~/test123.txt");


I should think that ~ is something that BASH/ZSH etc. is expanding to /Users/martijn but the OS calls are not.

Allright than, absulute path’s they must be :wink:


Again, the File class helps you here:

auto filePath = File::getSpecialLocation (userHomeDirectory).getChildFile ("test123.txt");
myfile.open (filePath.getFullPathName.toRawUTF8());
1 Like

Yes it all makes sense now thanks again !!!