Skip to contentSkip to conversation

HOME directory

DISCUSSION Luciano Moreira - ht 1 participant 1 message Apr 25, 2005

lore
Share & tools
lore
#0 lore raw

Yeah ! You need to read the desired environment variable (like HOME), using a API like "|char *getenv(const char */name/);".

After read, you shall concatenate the 2 strings:

char *pHome;
char *pMyDir="mydir/mysubdir";
char sFullPath[_MAX_PATH];

pHome = getenv("HOME");
sprintf(sFullPath, "%s/%s", pHome, pMyDir);

WARNING: "sprintf()" isn't the faster way to build a concatenated string. If you'll need to repeat this in a long loop, try another way using only a single call to "strcat()".

Luciano
|

HIToC escreveu:

Quoted 15 lines

Hello all!
I am writing a piece of code that makes files, open directories in my
HOME directory. All worked well until I changed the name of my HOME dir. To make the code portable I tried with use of '~' character:

	const char*	filename = "~/file.txt";
	ofstream	 my_file(filename, ios::out);

but this solution does not work.
Have you any suggestion to find the path of the current home directory or the current user name?

Thanks for any suggestion.