Consider project in ~/my/computer/my_project
with files:
- main.cpp
- input
We want the project to be usable on ~/your/computer
so we use relative paths when dealing with file streams:
// main.cpp
#include <iostream>
#include <fstream>
int main ()
{
std::ifstream stream("input");
if (!stream.is_open())
{
std::cout << "No file detected" << std::endl;
}
stream.close();
}
This works when the user executes the program in the my_project
directory, but fails otherwise (prints No file detected
, which isn't desired).
How to make a program work regardless of the user's present working directory when using file streams like this?