gpt4 book ai didi

c - 如何让用户在 C 中回家(平台无关的方式)?

转载 作者:太空狗 更新时间:2023-10-29 15:25:04 24 4
gpt4 key购买 nike

如何在 C 中获取用户主页(与平台无关的方式)?

我可以在 linux 上使用 getenv("%HOME%") 并在 windows 上使用类似的字符串..但我需要一个独立于平台的方式?

最佳答案

如果未设置 $HOME,您也可以在 *nix 系统上使用 getpwuid():

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

int main() {
struct passwd *passwdEnt = getpwuid(getuid());
char *home = passwdEnt->pw_dir;
printf("home: %s\n", home);
return 0;
}

关于c - 如何让用户在 C 中回家(平台无关的方式)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5282502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com