954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Find CPU temperature with C code (in Linux)

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???

altic
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

Referring back several pages on the forum:

http://www.daniweb.com/forums/thread187421.html

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

^ 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

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

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:

altic
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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:

altic
Newbie Poster
8 posts since May 2009
Reputation Points: 10
Solved Threads: 0
 

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

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You