简体   繁体   English

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

[英]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: 您可以使用环境变量HOME ,如果不存在,则可以使用密码数据库:

#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. ~比如getenv("HOME")的扩展称为globbing,是你需要先做的事情。 You didn't say which libaries or frameworks you are using, but some provide this. 您没有说明您使用的是哪些库或框架,但有些提供此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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