电脑取名,不收费的宝宝取名平台?

总体概述:

在编写程序时,有时需要获取系统的主机名字,并根据当前主机名相应的处理。本章介绍在程序中调用C库函数,获取系统主机名的方法,并介绍函数的使用方法,编写示例程序。需要用到的函数有gethostname

函数语法:

gethostname函数语法:nt gethostname(char *name, size_t len);

函数作用:

gethostname函数:获取登录到进程控制终端的用户名。

参数介绍

gethostname函数参数介绍

  • 输出参数:name => 存储主机名的缓存。
  • 输入参数:len => 缓存长度。
  • 返回值:成功:返回 0;失败:返回 非0值。

代码示例:

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

int main(void)
{
    char hostname[64];

    if (gethostname(hostname, sizeof(hostname))) {
        printf("get hostname failed, %s.\n", strerror(errno));
        return -1;
    }
    printf("hostname: %s\n", hostname);
    
#if 0
    /* 上一篇,通过环境变量的方法获取 */
    const char *hostname = getenv("USER") ? : "can't find hostname";
    printf("hostname: %s\n", hostname);
#endif  
    return 0;
}

使用方法:

$ mkdir ~/clanguage && cd ~/clanguage
$ touch gethostname.c             注:创建并拷贝代码到gethostname.c,保存退出。
$ gcc -o gethostname gethostname.c
$ ./gethostname

运行效果:

电脑取名,不收费的宝宝取名平台?

运行效果图

点赞、收藏+关注获取更多精彩内容!

注:鉴于作者能力有限,文中错误与未尽事宜在所难免,恳请读者批评指正。

电脑取名,不收费的宝宝取名平台?

代码示例图

本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 673862431@qq.com 举报,一经查实,本站将立刻删除。
如若转载,请注明出处:https://www.xkfy8.com/archives/27857