tags:

views:

104

answers:

3

i'd like to put a kind of lock file in the user's home directory on linux(from c++) but fopen'ing ~/.fluudit doesn't seem to work.

fopen("~/.fluudit","w");   //fails
+1  A: 

The expansion of ~ to, say, getenv("HOME") is called globbing and is something you need to do first. You didn't say which libaries or frameworks you are using, but some provide this.

Dirk Eddelbuettel
A: 

Without knowing any C, I found this by googling: http://blog.crowdway.com/2009/09/16/expanding-a-leading-tilde-in-cc/

Marian
+3  A: 

You can use the environment variable HOME and if that's not present, you can use the password database:

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>

struct passwd *pw = getpwuid(getuid());

const char *homedir = pw->pw_dir;
R Samuel Klatchko