Relative path in fstream open() Mac OS X does not work

Viewed 2079

Does fstream's open() take relative or absolute file paths? Here is the example code -

#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main() {
    fstream myfile;

    myfile.open("subd/example.txt");
    if(myfile.is_open()) {
            for(int i = 0; i < 10; i++) {
                    myfile << "Example data " << i << endl;
            }
    } else cout << "Unable to open " << endl;
    myfile.close();
    return 0;
}

I working on a MAC OS X environment. The subdirectory already exists. .open() returns false for each of these cases. I am also using -std=c++11. How do I get around this problem? The permissions for the 'subd' directory are :

enter image description here

The output is always "Unable to open". The solution of the original post(the one this post is marked the duplicate of) does not solve my problem.

Solution, because none of the responses below completely resolve my issue

Use ofstream with the relative path(with respect to the location of the program executable ie. argv[0]). fstream just doesn't seem to work the same way.

1 Answers
Related