how to open a file in user's home folder
i'd like to put a kind of lock file in the user's home directory on linu开发者_开发技巧x(from c++) but fopen'ing ~/.fluudit doesn't seem to work.
fopen("~/.fluudit","w"); //fails
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;
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.
精彩评论