如何在用户的主文件夹中打开文件

时间:2022-02-22 00:30:15

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.

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

fopen("~/.fluudit","w");   //fails

2 个解决方案

#1


10  

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

您可以使用环境变量HOME,如果不存在,您可以使用密码数据库:

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

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

const char *homedir = pw->pw_dir;

#2


4  

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.

~~比如getenv(“HOME”)的扩展被称为通配,是你需要先做的事情。您没有说明您正在使用哪些库或框架,但有些提供此功能。

#1


10  

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

您可以使用环境变量HOME,如果不存在,您可以使用密码数据库:

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

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

const char *homedir = pw->pw_dir;

#2


4  

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.

~~比如getenv(“HOME”)的扩展被称为通配,是你需要先做的事情。您没有说明您正在使用哪些库或框架,但有些提供此功能。