1
#include <string>
#include <stdio.h>
#include <pwd.h>

std::string impPath()
{
    char *name;
    struct passwd *pass;
    pass = getpwuid(getuid());
    name = pass->pw_name;

    std::string PATH = "/home";
    PATH.append("/");
    PATH.append(name);

    return PATH;
}

我需要知道用户的用户名。为此。我正在使用getpwuid(),但出现此错误。

/home/shobhit/Desktop/file.cpp:15: error: 'getuid' was not declared in this scope
 pass = getpwuid(getuid());
                        ^

我只是无法弄清楚getuid未在此范围内声明的原因是什么。我想我已经包含了所有必要的头文件。(yami 对 R 的答案getlogin() c 函数的评论返回 NULL 和错误“没有这样的文件或目录” 我尝试在网上搜索但找不到任何相关的答案。

4

2 回答 2

8

man getuid

概要

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

uid_t getuid(void);
uid_t geteuid(void);

你错过了那些包括。

于 2018-06-16T13:06:09.140 回答
1

getuid()中不存在pwd.h。见http://pubs.opengroup.org/onlinepubs/7908799/xsh/pwd.h.html

于 2018-06-16T13:07:02.793 回答