Hey there, please help me how to solve this problem. I need to find my CPU temperature in C code on Linux for my new program. Can anyone tell me???

^ from that page:

on linux you would just grep sysctl for a block of info

$ sysctl -a |grep therm
...

or query for the specific variable

$  sysctl hw.acpi.thermal.tz0.temperature
hw.acpi.thermal.tz0.temperature: 54.0C
Comments
Good find!

well, its solved in C++ code. here its go,,,,

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(void)
{
  for( ; ; ){
    string therm;
    int i=0;
    ifstream inFile("/proc/acpi/thermal_zone/THRM/temperature");
    if (inFile.fail()){
      cerr <<"File tidak bisa dibuka"<< endl;
      exit(1);
    }  
    while (inFile >> therm) {
      i++;
      if(i==2)
        cout<<"Temperature adalah "<<therm<<endl;
    }
    inFile.close();
  }  
  return 0;
}

:cool: :cool: :cool:

well, its solved in C++ code. here its go,,,,

#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
int main(void)
{
  for(;;){
    string therm;
    int i=0;
    ifstream inFile("/proc/acpi/thermal_zone/THRM/temperature");
    if (inFile.fail()){
      cerr <<"File cannot be read!"<< endl;
      exit(1);
    }  
    while (inFile >> therm) {
      i++;
      if(i==2)
        cout<<"The Themperature is "<<therm<<endl;
    }
    inFile.close();
  }  
  return 0;
}

:cool: :cool: :cool:

#include <stdio.h> 
#include <stdlib.h> 

int main() { 

    FILE *f; 
    char c; 
    f=fopen("/proc/acpi/thermal_zone/CPUZ/temperature","rt"); 

    while((c=fgetc(f))!=EOF){ 
        printf("%c",c); 
    } 

    fclose(f); 
    return 0; 
} 
Comments
4 year old thread
This question has already been answered. Start a new discussion instead.