在树莓派下获取CPU温度示例

//源码来自http://blog.csdn.net/xukai871105/article/details/38349209
//修改了打开文件用的代码等
//感谢@xukai871105
//死都做不到无限循环来监视……不知为何

#include 
#include 

#include 
#include 
#include 

#define MAX_SIZE 32

int main(int argc,char *argv[])
{
        FILE *fp;
        double temp = .0;
        char buf[MAX_SIZE];

        //打开/sys/class/thermal/thermal_zone0/temp
        fp = fopen("/sys/class/thermal/thermal_zone0/temp","r");
        if (fp == NULL)
        {
                puts("failed to open thermal_zone0/temp\\n");
                exit(0);
        }

        //读取内容并输出
        char *p = fgets(buf,15,fp);
        while(p != NULL)
        {
                printf("%.2f",((float)atoi(buf))/1000);
                p = fgets(buf,15,fp);
        }
        printf("\\n");

        //关闭文件
        int return_value = fclose(fp);
        if (return_value != 0)
        {
                puts("文件关闭失败\\n");
                exit(0);
        }
        return 0;
}

源码来自: CSDN博客
感谢@xukai871105

你可能感兴趣的:(在树莓派下获取CPU温度示例)