大家好,我是杂烩君。
本次来分享几个实用的代码小片段。
获取CPU温度
应用可以定时获取CPU的温度,比如程序异常崩溃时,我们可能需要分析多方面原因,CPU温度就是其中之一。
代码:
左右滑动查看全部代码>>>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#define CPU_TEMP_FILE0 "/sys/devices/virtual/thermal/thermal_zone0/temp"
struct cpu_temperature
{
int integer_part;
int decimal_part;
};
typedef struct cpu_temperature cpu_temperature_t;
cpu_temperature_t get_cpu_temperature(const char *_cpu_temp_file)
{
FILE *fp = NULL;
cpu_temperature_t cpu_temperature = {0};
int temp = 0;
fp = fopen(_cpu_temp_file, "r");
if (NULL == fp)
{
printf("fopen file error\n");
return cpu_temperature;
}
fscanf(fp, "%d", &temp);
cpu_temperature.integer_part = temp / 1000;
cpu_temperature.decimal_part = temp % 1000 / 100;
fclose(fp);
return cpu_temperature;
}
int main(int arc, char *argv[])
{
cpu_temperature_t cpu_temperature = {0};
cpu_temperature = get_cpu_temperature(CPU_TEMP_FILE0);
printf("cpu_temperature = %d.%d ℃\n", cpu_temperature.integer_part, cpu_temperature.decimal_part);
return 0;
}
运行结果:
获取文件大小
有时候我们需要获取某个文件的大小,比如如果需要发送文件里的内容,则需要知道文件的大小。
代码:
左右滑动查看全部代码>>>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
long get_file_size(const char *_file_name)
{
FILE * fp = fopen(_file_name, "r");
if (NULL == fp)
{
printf("fopen error\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
long size = ftell(fp);
fclose(fp);
return size;
}
int main()
{
#define FILE_NAME "./get_file_size"
long file_size = get_file_size(FILE_NAME);
printf("file_size = %ld\n", file_size);
return 0;
}
运行结果:
获取时间戳
系统时间戳很常用,比如log输出时,可以附带时间戳数据,方便分析。
代码:
左右滑动查看全部代码>>>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
long long get_sys_time_ms(void)
{
long long time_ms = 0;
struct timeval sys_current_time;
gettimeofday(&sys_current_time, NULL);
time_ms = ((long long)sys_current_time.tv_sec*1000000 + sys_current_time.tv_usec) / 1000;
return time_ms;
}
int main(int arc, char *argv[])
{
long long cur_sys_time = get_sys_time_ms();
printf("cur_sys_time = %lld ms\n", cur_sys_time);
return 0;
}
运行结果:
获取MAC
MAC地址,有时候会作为设备ID实用,作为设备唯一标识。
代码:
左右滑动查看全部代码>>>
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int get_netif_mac(const char *_ifr_name, uint8_t *_mac)
{
int32_t ret = -1;
struct ifreq m_ifreq;
int32_t sock = 0;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
{
printf("socket err\r\n");
goto err;
}
strcpy(m_ifreq.ifr_name, _ifr_name);
ret = ioctl(sock,SIOCGIFHWADDR, &m_ifreq);
if (ret < 0)
{
printf("ioctl err:%d\r\n",ret);
goto err;
}
snprintf((char *)_mac, 32, "%02x%02x%02x%02x%02x%02x", (uint8_t)m_ifreq.ifr_hwaddr.sa_data[0],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[1],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[2],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[3],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[4],
(uint8_t)m_ifreq.ifr_hwaddr.sa_data[5]);
return 0;
err:
return -1;
}
int main(int argc, char **argv)
{
char mac_str[32] = {0};
get_netif_mac("wlan1", mac_str);
printf("mac = %s\n", mac_str);
return 0;
}
运行结果:
获取IP
有时候需要获取本机IP进行显示。
代码:
左右滑动查看全部代码>>>
#include <stdio.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
int get_local_ip(const char *_ifr_name, char *_ip)
{
int ret = -1;
int sockfd;
struct sockaddr_in sin;
struct ifreq ifr;
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
if (-1 == sockfd)
{
printf("socket error\n");
return ret;
}
strncpy(ifr.ifr_name, _ifr_name, IFNAMSIZ);
ifr.ifr_name[IFNAMSIZ - 1] = 0;
if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
{
printf("ioctl error\n");
close(sockfd);
return ret;
}
memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
int ip_len = snprintf(_ip, 32, "%s", inet_ntoa(sin.sin_addr));
close(sockfd);
ret = ip_len;
return ret;
}
int main(int argc, char **argv)
{
char ip_str[32] = {0};
get_local_ip("wlan1", ip_str);
printf("ip = %s\n", ip_str);
return 0;
}
运行结果:
以上就是本次分享的几个小的代码片段。
如果觉得文章有帮助,麻烦帮忙转发,谢谢!