gpt4 book ai didi

C++ 获取 linux 发行版名称\版本

转载 作者:IT王子 更新时间:2023-10-29 00:16:18 25 4
gpt4 key购买 nike

根据问题“How to get Linux distribution name and version?”,要获取 Linux 发行版名称和版本,这是可行的:

lsb_release -a

在我的系统上,它显示了所需的输出:

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 9.10
Release: 9.10
Codename: karmic

现在,要在 C++ 中获取此信息,Qt4 的 QProcess 将是一个不错的选择,但由于我在没有 Qt 的情况下使用标准 c++ 进行开发,我需要知道如何在标准 C++ 中获取此信息,即进程的标准输出,以及一种解析信息的方法。

到目前为止,我正在尝试使用来自 here 的代码但我坚持功能 read ().

最佳答案

你可以简单地使用函数:

int uname(struct utsname *buf);

通过包含标题

#include <sys/utsname.h>

它已经将名称和版本作为结构的一部分返回:

   struct utsname 
{
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined network" */
char release[]; /* OS release (e.g., "2.6.28") */
char version[]; /* OS version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};

我错过了什么吗?

关于C++ 获取 linux 发行版名称\版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6315666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com