#include<stdlib.h>
#include<stdio.h>
#include <string.h>
typedef struct _CPU_INFO
{
char Company[256];
char Model[256];
char Family[256];
char Brand[512];
}CPUINFO,*PCPU_INFO;
void FillCPUStruct(PCPU_INFO cpuInfo)
{
FILE *fp;
int nread = 0;
char *file = "/proc/cpuinfo";
char *buffer = NULL;
char content[40]="";
size_t len = 0;
fp = fopen(file,"rb");
if(fp == NULL)
{
printf("error happen to open:%s\n",file);
return;
}
while(nread=getline(&buffer,&len,fp)!=-1)
{
if(strstr(buffer,"vendor_id")!=NULL)
{
buffer[strlen(buffer)-1]=0;
sscanf(buffer,"%s%s%s",content,content,content);
strcpy(cpuInfo->Company,content);
}
if(strstr(buffer,"cpu family")!=NULL)
{
buffer[strlen(buffer)-1]=0;
//printf("len:buffer=%d:%d\n",len,strlen(buffer));
sscanf(buffer,"%s%s%s%s",content,content,content,content);
strcpy(cpuInfo->Family,content);
//printf("cpu family=%s\n",content);
}
if(strstr(buffer,"model")!=NULL)
{
if(strstr(buffer,"model name")!=NULL)
{
buffer[strlen(buffer)-1]=0;
//printf("model name buffer=%s\n",buffer);
char *ptr = strchr(buffer,':');
//printf("model name buffer=%s\n",buffer);
//strncpy(content,(char*)&buffer+strstr(buffer,":"),strlen(buffer));
//sscanf(buffer,"%s%s%s%s",content,content,content,content);
strcpy(content,buffer+(ptr-buffer)+2);
strcpy(cpuInfo->Brand,content);
//printf("model name=%s\n",buffer+(ptr-buffer)+2);
}else{
buffer[strlen(buffer)-1]=0;
//printf("model buffer=%s\n",buffer);
sscanf(buffer,"%s%s%s",content,content,content);
strcpy(cpuInfo->Model,content);
//printf("model=%s\n",content);
}
}
}
}
void print_cpu_info(PCPU_INFO cpustruct)
{
if(cpustruct == NULL)
return;
printf("CPU Information:\n");
printf("\tCPU Manufacturer: %s\n", cpustruct->Company);
printf("\tCPU Model: %s\n", cpustruct->Model);
printf("\tCPU CPU Family: %s\n", cpustruct->Family);
printf("\tCPU Brand Name: %s\n", cpustruct->Brand);
//printf("\tCPU Info hash value: %s\n", cpustruct->md5_hash);
}
void InitCPUStruct(PCPU_INFO CPUStruct)
{
if(CPUStruct == NULL)
{
return;
}
memset(CPUStruct,0,sizeof(*CPUStruct));
}
int main()
{ CPUINFO cpu_info;
FillCPUStruct(&cpu_info);
print_cpu_info(&cpu_info);
}
读取/proc/cpuinfo获取CPU信息demo
3星 · 超过75%的资源 需积分: 47 196 浏览量
2017-07-13
17:59:23
上传
评论 1
收藏 4KB RAR 举报
飞奔的热干面
- 粉丝: 18
- 资源: 5
最新资源
- 基于SpringBoot Mybatis-Plus TypeScript的微服务多租户SaaS管理快速开发框架 .zip
- 论文复现:QA-GNN: Reasoning with Language Models and Knowledge
- ipp(intel-oneAPI)下载地址.txt
- 基于spring-boot dubbox搭建的java分布式系统的前端管理.zip
- VLC+Qt demoVLC+Qt demo
- 海彪&龙梅子 - 寂寞的人伤心的歌 (DJ版) [mqms2].ogg
- 530springboot + vue 旅游管理系统.zip(可运行源码+数据库文件+文档)
- 基于SpringBoot + Thymeleaf + Layui + Apache Shiro 的后台管理系统 .zip
- 表1:长江大学文理学院课外学分申请表.et
- base.apk
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈