From 116a29438b7c5438df8c0d2e76814627b6612ffb Mon Sep 17 00:00:00 2001 From: Felix Moser <felix.moser@informatik.hs-fulda.de> Date: Fri, 24 Jan 2025 16:31:58 +0100 Subject: [PATCH] reading cpu temperature --- main.c | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index e1988a3..6d879eb 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,27 @@ #include <stdio.h> +#include <stdlib.h> + +void read_cpu_temp() { + FILE *file; + char buffer[1024]; + char *temp_path = "/sys/class/thermal/thermal_zone0/temp"; + + file = fopen(temp_path, "r"); + if (file == NULL) { + printf("Error opening file\n"); + return; + } + + if (fgets(buffer, sizeof(buffer), file) != NULL) { + long temp = atol(buffer) / 1000; + printf("CPU temperature: %ld C\n", temp); + } else { + printf("Error reading file\n"); + } + fclose(file); +} int main(void) { - printf("Hello, World!\n"); + read_cpu_temp(); return 0; } \ No newline at end of file -- GitLab