c - 如何在 linux 中从/proc/cpuinfo 获取模型名称?

标签 c linux linux-kernel kernel

我在 C 中工作,并试图从 /proc 文件中获取特定信息。 我知道在 Linux 中,当我执行以下操作时,我会得到模型名称。

cd /proc
cat cpuinfo | grep 'model name'

但如果我尝试在 C 中执行此操作,则会导致 core dumped

thisfile = fopen("/proc/cpuinfo | grep 'model name' ", "r");

如何在打开文件时获取模型名称?

最佳答案

fopen("/proc/cpuinfo | grep 'model name' ", "r"); 将返回 NULL 指针,因为文件 /proc/cpuinfo | grep '型号名称'肯定不存在

fopen 允许打开一个文件,而不是执行命令

使用popen:

FILE * fp = popen("grep 'model name'  /proc/cpuinfo", "r");

if (fp != NULL) {
  ...read in 
  pclose(fp);
}

关于c - 如何在 linux 中从/proc/cpuinfo 获取模型名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54782308/

相关文章:

java - 如何通过curl从Oracle下载jdk文件?

c - 如何在C语言中打印函数中的变量名称

c - 需要帮助理解在 C 中使用 struct 的错误

c++ - 文件处理 : the code stops running

linux - 如何用sed删除与模式匹配的行及其后的行?

Linux 操作系统/英特尔 64 位架构的 CPU 温度

c中复杂的函数声明

android - 如何使内联函数在可执行文件中只占用一份内存空间

linux - 分析 Linux 内核模块

c - 在哪里编写中断处理程序以及如何在 Linux 中向内核注册?