如何在Unix中打印当前用户和系统名称?

Com*_*org 4 c++ unix shell username

我期待着学习如何在Unix中打印当前登录的用户和系统名称.

#include <unistd.h>
#include <fcntl.h>

using namespace std;

int main(int argc, char **argv)   
{      
     //Print the current logged-in user / username.   
     //Print the name of the system / computer name.

     return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果您能提供一行或两行代码作为演示,我将不胜感激.谢谢

fge*_*fge 5

用户 - > getuid()(另见geteuid()).

机器名称 - > gethostname().

这是纯粹的C.我不知道C++是否有其他库调用.

  • 我不会称之为"纯C".这些函数由POSIX定义,而不是由C标准定义,并且它们可能在某些系统(例如Windows)上不可用.(是的,我知道这个问题被标记为"unix".) (2认同)