[应用相关] 分享几个实用的代码片段(附代码例子)

[复制链接]
 楼主| 盗铃何须掩耳 发表于 2022-7-25 12:00 | 显示全部楼层 |阅读模式
获取CPU温度

应用可以定时获取CPU的温度,比如程序异常崩溃时,我们可能需要分析多方面原因,CPU温度就是其中之一。

代码:


  1. #include <stdio.h>   
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>

  6. #define CPU_TEMP_FILE0 "/sys/devices/virtual/thermal/thermal_zone0/temp"

  7. struct cpu_temperature
  8. {
  9. int integer_part;
  10. int decimal_part;
  11. };

  12. typedef struct cpu_temperature cpu_temperature_t;

  13. cpu_temperature_t get_cpu_temperature(const char *_cpu_temp_file)
  14. {
  15. FILE *fp = NULL;
  16. cpu_temperature_t cpu_temperature = {0};
  17. int temp = 0;

  18. fp = fopen(_cpu_temp_file, "r");
  19. if (NULL == fp)
  20. {
  21.   printf("fopen file error\n");
  22.   return cpu_temperature;
  23. }

  24. fscanf(fp, "%d", &temp);
  25. cpu_temperature.integer_part = temp / 1000;
  26. cpu_temperature.decimal_part = temp % 1000 / 100;

  27. fclose(fp);

  28. return cpu_temperature;
  29. }


  30. int main(int arc, char *argv[])
  31. {
  32. cpu_temperature_t cpu_temperature = {0};

  33. cpu_temperature = get_cpu_temperature(CPU_TEMP_FILE0);
  34. printf("cpu_temperature = %d.%d ℃\n", cpu_temperature.integer_part, cpu_temperature.decimal_part);
  35. return 0;
  36. }
复制代码

运行结果:

8070162de15630f119.png


 楼主| 盗铃何须掩耳 发表于 2022-7-25 12:02 | 显示全部楼层
获取文件大小

有时候我们需要获取某个文件的大小,比如如果需要发送文件里的内容,则需要知道文件的大小。

代码:

  1. #include <sys/stat.h>  
  2. #include <unistd.h>  
  3. #include <stdio.h>  

  4. long get_file_size(const char *_file_name)
  5. {
  6.     FILE * fp = fopen(_file_name, "r");
  7.     if (NULL == fp)
  8.     {
  9.         printf("fopen error\n");
  10.         return -1;
  11.     }

  12.     fseek(fp, 0L, SEEK_END);
  13.     long size = ftell(fp);
  14.     fclose(fp);

  15.     return size;
  16. }

  17. int main()
  18. {
  19.     #define FILE_NAME  "./get_file_size"
  20.     long file_size = get_file_size(FILE_NAME);
  21.     printf("file_size = %ld\n", file_size);

  22.     return 0;
  23. }
复制代码

2533362de15823ba2f.png


 楼主| 盗铃何须掩耳 发表于 2022-7-25 12:05 | 显示全部楼层
获取时间戳

系统时间戳很常用,比如log输出时,可以附带时间戳数据,方便分析。

代码:

  1. #include <stdio.h>   
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <errno.h>
  6. #include <sys/time.h>
  7. #include <time.h>

  8. long long get_sys_time_ms(void)
  9. {
  10.     long long time_ms = 0;
  11.     struct timeval sys_current_time;

  12.     gettimeofday(&sys_current_time, NULL);
  13.     time_ms = ((long long)sys_current_time.tv_sec*1000000 + sys_current_time.tv_usec) / 1000;

  14.     return time_ms;
  15. }

  16. int main(int arc, char *argv[])
  17. {
  18. long long cur_sys_time = get_sys_time_ms();

  19.     printf("cur_sys_time = %lld ms\n", cur_sys_time);

  20. return 0;
  21. }
复制代码

9505762de15ecbf837.png


 楼主| 盗铃何须掩耳 发表于 2022-7-25 12:07 | 显示全部楼层
获取MAC

MAC地址,有时候会作为设备ID实用,作为设备唯一标识。

代码:

  1. #include <stdio.h>
  2. #include <net/if.h>
  3. #include <sys/ioctl.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include <string.h>

  7. int get_netif_mac(const char *_ifr_name, uint8_t *_mac)
  8. {
  9. int32_t    ret = -1;
  10.     struct ifreq   m_ifreq;
  11.     int32_t    sock = 0;

  12.     sock = socket(AF_INET, SOCK_STREAM, 0);
  13. if (sock < 0)
  14. {
  15.   printf("socket err\r\n");
  16.   goto err;
  17. }

  18.     strcpy(m_ifreq.ifr_name, _ifr_name);

  19.     ret = ioctl(sock,SIOCGIFHWADDR, &m_ifreq);
  20. if (ret < 0)
  21. {
  22.   printf("ioctl err:%d\r\n",ret);
  23.   goto err;
  24. }

  25.     snprintf((char *)_mac, 32, "%02x%02x%02x%02x%02x%02x", (uint8_t)m_ifreq.ifr_hwaddr.sa_data[0],
  26.                                                      (uint8_t)m_ifreq.ifr_hwaddr.sa_data[1],
  27.                                                      (uint8_t)m_ifreq.ifr_hwaddr.sa_data[2],
  28.                                                      (uint8_t)m_ifreq.ifr_hwaddr.sa_data[3],
  29.                                                      (uint8_t)m_ifreq.ifr_hwaddr.sa_data[4],
  30.                                                      (uint8_t)m_ifreq.ifr_hwaddr.sa_data[5]);

  31.     return 0;
  32. err:
  33. return -1;
  34. }


  35. int main(int argc, char **argv)
  36. {
  37.     char mac_str[32] = {0};
  38.     get_netif_mac("wlan1", mac_str);
  39.     printf("mac = %s\n", mac_str);

  40.     return 0;
  41. }
复制代码

8885862de16b71c83e.png


 楼主| 盗铃何须掩耳 发表于 2022-7-25 12:11 | 显示全部楼层
获取IP

有时候需要获取本机IP进行显示。

代码:

  1. #include <stdio.h>
  2. #include <net/if.h>
  3. #include <sys/ioctl.h>
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #include <string.h>

  7. int get_local_ip(const char *_ifr_name, char *_ip)
  8. {
  9. int ret = -1;
  10.     int sockfd;
  11.     struct sockaddr_in sin;
  12.     struct ifreq ifr;

  13.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  14.     if (-1 == sockfd)
  15.     {
  16.         printf("socket error\n");
  17.         return ret;
  18.     }

  19.     strncpy(ifr.ifr_name, _ifr_name, IFNAMSIZ);
  20.     ifr.ifr_name[IFNAMSIZ - 1] = 0;

  21.     if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
  22.     {
  23.         printf("ioctl error\n");
  24.         close(sockfd);
  25.         return ret;
  26.     }

  27.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  28.     int ip_len = snprintf(_ip, 32, "%s", inet_ntoa(sin.sin_addr));

  29.     close(sockfd);
  30. ret = ip_len;

  31. return ret;
  32. }

  33. int main(int argc, char **argv)
  34. {
  35.     char ip_str[32] = {0};
  36.     get_local_ip("wlan1", ip_str);
  37.     printf("ip = %s\n", ip_str);

  38.     return 0;
  39. }
复制代码

909862de1719e54b4.png


麻花油条 发表于 2022-7-25 14:39 来自手机 | 显示全部楼层
确实会经常用到,感谢分享
天灵灵地灵灵 发表于 2022-7-25 18:52 | 显示全部楼层
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则

50

主题

385

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部