如何在Unix中打印当前用户和系统名称? [英] How to print the current user and system name in Unix?

查看:144
本文介绍了如何在Unix中打印当前用户和系统名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Please i am looking forward to learn how to print the current logged-in user and system name in 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;
}

如果您可以提供一行代码或两行代码。感谢

I would be grateful if you can provide a line of code or two as demonstration. Thanks

推荐答案

getuid()要获取用户名,您必须另外使用 getpwuid()

getuid() gets the id not the username. To get the username you'll have to additionally use getpwuid():

struct passwd *passwd;
passwd = getpwuid ( getuid()); 

printf("The Login Name is %s ", passwd->pw_name);

查看

为获取主机名,您可以使用 gethostname()函数。

And for getting the hostname you can use the gethostname() function.

这篇关于如何在Unix中打印当前用户和系统名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆