Open ShellAlbert opened 5 years ago
#include <stdlib.h> /* for atof */
int main (int argc, char **argv) {
FILE *filePtr;
char therm_path[64]; /* 64 bytes buffer for building current pseudo file path */
char readBuf[16]; /* 16 bytes buffer for reading ASCII encoded value from pseudo file */
float temp;
for (unsigned int curZone = 0; curZone < 8; curZone++) {
unsigned int readBytes = 0;
sprintf(therm_path, "/sys/devices/virtual/thermal/thermal_zone%u/temp", curZone);
printf ("Reading %s: ", therm_path);
filePtr = fopen(therm_path, "r");
if (!filePtr) {
printf ("Error, failed to open file\n");
continue;
}
while (readBytes < 16) {
int curRead = fread(readBuf + readBytes, 1, 16 - readBytes, filePtr);
if (curRead > 0)
readBytes += curRead;
else
break; /* nothing more to be read */
}
fclose (filePtr);
if (readBytes > 0)
*(readBuf + readBytes - 1) = 0; /* Remove last char '\n' */
printf("[%s] ", readBuf);
temp = atof(readBuf);
temp /= 1000.f;
printf("%.02f\n", temp);
}
exit(0);
}