Using std::ifstream and std::ofstream with Min?

Joe Kaplan's icon

Greetings! I would like my external to create, read, and write a text file on the disk.

I was able to implement this using std::ifstream() and std::ofstream().

When I tested it in debug mode, launching max through Visual Studio, it works!
However, when I launch max the normal way, then add my external to a patch, and send a message to call this function, Max crashes.

I think this might be because the my external is confused about the application root.
I am not specifying a path in my code. I am just writing:

std::ofstream newFile;
newFile.open("myFile.txt");

When I launch in debug mode through Visual Studio, the file gets created. But it's creating the file in the same folder as the .sln file for the external. As in:
"C:\Users\Joe\Documents\Max 8\Packages\MyPackage\build\source\projects\MyExternal.Hello_world\myfile.txt"

This path is certainly not available when the external is running production. I figured the external would think the application root is either the location of max.exe, or the location of the .maxpat file, or the location of the .mxe64 file.

But it seems like like the external might have no idea what the application root is and just gives up.


Can anybody suggest a clean solution for this? Thanks!