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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With