当前位置:   article > 正文

分享几个实用的代码片段(附代码例子)_分享代码

分享代码

大家好,我是杂烩君。

本次来分享几个实用的代码小片段。

获取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.  
  19.  fp = fopen(_cpu_temp_file, "r");
  20.  if (NULL == fp)
  21.  {
  22.   printf("fopen file error\n");
  23.   return cpu_temperature;
  24.  }
  25.  fscanf(fp, "%d", &temp);
  26.  cpu_temperature.integer_part = temp / 1000;
  27.  cpu_temperature.decimal_part = temp % 1000 / 100;
  28.  fclose(fp);
  29.  return cpu_temperature;
  30. }
  31. int main(int arc, char *argv[])
  32. {
  33.  cpu_temperature_t cpu_temperature = {0};
  34.  cpu_temperature = get_cpu_temperature(CPU_TEMP_FILE0);
  35.  printf("cpu_temperature = %d.%d ℃\n", cpu_temperature.integer_part, cpu_temperature.decimal_part);
  36.  return 0;
  37. }

运行结果:

41412a60fbc86cba6b8ce7c153e7d1f7.png

获取文件大小

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

代码:

左右滑动查看全部代码>>>

  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. }

运行结果:

3be4bd263a60b279919c4fc4b8cd7e33.png

获取时间戳

系统时间戳很常用,比如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. }

运行结果:

c38faae84f648e7d184d77bdb8dd32c9.png

获取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. }

运行结果:

206ba851722eee8a53eae21944ccba5c.png

获取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.  
  14.     sockfd = socket(AF_INET, SOCK_DGRAM, 0);
  15.     if (-1 == sockfd)
  16.     {
  17.         printf("socket error\n");
  18.         return ret;
  19.     }
  20.  
  21.     strncpy(ifr.ifr_name, _ifr_name, IFNAMSIZ);
  22.     ifr.ifr_name[IFNAMSIZ - 1] = 0;
  23.  
  24.     if (ioctl(sockfd, SIOCGIFADDR, &ifr) < 0)
  25.     {
  26.         printf("ioctl error\n");
  27.         close(sockfd);
  28.         return ret;
  29.     }
  30.  
  31.     memcpy(&sin, &ifr.ifr_addr, sizeof(sin));
  32.     int ip_len = snprintf(_ip, 32"%s", inet_ntoa(sin.sin_addr));
  33.  
  34.     close(sockfd);
  35.  ret = ip_len;
  36.  return ret;
  37. }
  38. int main(int argc, char **argv)
  39. {
  40.     char ip_str[32] = {0};
  41.     get_local_ip("wlan1", ip_str);
  42.     printf("ip = %s\n", ip_str);
  43.     return 0;
  44. }

运行结果:

463cd0faf3321c04c703eaf2db7ed0ec.png

以上就是本次分享的几个小的代码片段。

如果觉得文章有帮助,麻烦帮忙转发,谢谢!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/756511
推荐阅读
相关标签
  

闽ICP备14008679号