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
commented: Good find! +7

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:

i thought this thread was "Find CPU temperature with C code"

#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; 
} 
commented: 4 year old thread -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.