-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_temp3.c
52 lines (47 loc) · 1.26 KB
/
cpu_temp3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
#define MAX_SIZE 32
#define END_PATH "/home/pi/file.txt"
//#define END_PATH "/home/pi/1.txt"
int main(void)
{
int fd;
double temp = 0;
char buf[MAX_SIZE];
char command[256] = {0};
float val ;
int fd1;
ssize_t nrd;
// ../sys/class/thermal/thermal_zone0/temp
fd = open(TEMP_PATH, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "failed to open thermal_zone0/temp\n");
return -1;
}
// ....
// if (read(fd, buf, MAX_SIZE) < 0) {
// fprintf(stderr, "failed to read temp\n");
// return -1;
// }
fd1 = open(END_PATH, O_APPEND | O_WRONLY, S_IRUSR);//20160517 O_CREAT append means add something to the end of this file
if (fd1 < 0 ) {
fprintf(stderr,"file not exist\n");
}
while(nrd = read(fd,buf,MAX_SIZE)){
write(fd1,buf,nrd);
}
// ........
temp = atoi(buf) / 1000.0;
printf("temp: %.2f\n",temp);
//val = (float)temp;
//snprintf(command, 256, "echo %.2f >> /home/pi/file.txt", val);
//system(command);
// ....
close(fd);
close(fd1);
}