How do I have to describe an absolute path to a file in C++?

I want to do a program in C++ that, when is executed (from anywhere), modifies a text file in a very specific path (and always the same one). To do so, I’ve defined the following function in C++:

void ChangeCourse(string course)
    ofstream active_course;"~/.universidad/curso_activo.txt");

    if (!curso_activo)
        cout << "Error: cant't open the file" << endl;
        active_course << course;

When executing the program that calls the function, I get the message Error: cant't open the file and, indeed, no file is created.

How do I have to define the path of the file such a way the program could read it and find it, regardless of where the program was called.

Note that I’m running the program in macOS.

Hope I’ve explained my self and thank you in advance.


In C++17 you can use the filesystem library and hard code the path, if this is what you really want to do.

#include <filesystem>
#include <fstream>

namespace fs=std::filesystem;

int main() {

  fs::path p = "your-absolute-path-goes-here"; // 
  if(fs::exists(p)) {
    std::ofstream file(p);
    file << "Hello world!" << "n";
  return 0;