简体   繁体   中英

how to open a file in user's home folder

我想在linux上的用户主目录中放置一种锁文件(来自c ++),但fopen'ing~ / .fluudit似乎不起作用。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM