You should use std::string
as:
std::string appData = getenv("APPDATA");
std::string path = appData+"\\MyApplication\\hello.txt";
then do this:
const char * mypath = path.c_str();
Note that you must not do this:
const char* mypath = (appData+"\\MyApplication\\hello.txt").c_str();
It is because expression on the right hand side is a temporary which gets destroyed at the end of the expression and mypath
will continue to point to the destroyed object. It becomes a dangling pointer, in other words.
--
Why is mypath changed in that weird string ?
Because in your posted code, mypath
is a dangling pointer, using which invokes undefined behavior.
This is how you should write the code:
std::string appData = getenv("APPDATA");
std::string mypath= appData+"\\MyApplication\\hello.txt";
cout << mypath;
fstream file(mypath.c_str(),ios::in);