【问题标题】:List all users names of the Linux system by calling c++ function [closed]通过调用c ++函数列出Linux系统的所有用户名[关闭]
【发布时间】:2019-01-02 21:19:15
【问题描述】:

我想通过调用某个库中的 c++ 函数来获取 Posix 系统的所有用户名。找到使用来自 c++ 程序的List all Linux users without systen users 之类的调用 bash 命令的唯一选项

【问题讨论】:

  • C++ 不了解 linux,它独立于操作系统。您需要使用系统调用或获取库。
  • 也许您正在寻找getpwent
  • @NathanOliver 同意我编辑了我的问题

标签: c++ linux


【解决方案1】:

POSIX 通过getpwent 函数提供此功能:

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

int main() {
    while (true) {
        errno = 0; // so we can distinguish errors from no more entries
        passwd* entry = getpwent();
        if (!entry) {
            if (errno) {
                std::cerr << "Error reading password database\n";
                return EXIT_FAILURE;
            }
            break;
        }
        std::cout << entry->pw_name << '\n';
    }
    endpwent();
}

【讨论】:

  • 这正是我想要的!谢谢!
猜你喜欢
  • 2017-01-21
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 2020-01-04
  • 1970-01-01
  • 2016-07-15
  • 2011-07-26
  • 2015-09-17
相关资源
最近更新 更多