How to obtain filesystem path information from OFstream in OpenFOAM

When parent path or other path information is needed from a file stream within a function in OpenFOAM.

For details about ‘OFstream’ in C++, please visit this reference.

#include "fvCFD.H"
 
void get_parent_path_from_file(OFstream& ofile)
{
    // Get parent path from file
    fileName filePath(ofile.name());
    return filePath.path();
}
 
int main(int argc, char **argv)
{
    #include "setRootCase.H"
    #include "createTime.H"
    #include "createMesh.H"
 
    // Get the case path from the `argList` args global variable.
    std::string casePath = args.rootPath() + "/" + args.globalCaseName();
 
    // Open the file in the case directory.   
    OFstream errorFile(casePath + "/errorFileName.csv");
    errorFile << "COLUMN_A,COLUMN_B,COLUMN_C" << endl;
 
    Info << get_parent_from_file(errorFile);
     
}

See also