首页 互联网 分享几个实用的代码片段(附代码例子)

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

文章末尾附有学习资源,您可以自行获取。

获取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_temporal{ int integer_part;} ;typedef struct cpu_temp_file){ FILE *fp=NULL; cpu_temperture_t cpu_temper=0}; fp=fopen(_cpu_temp_file, \’r\’);=fp) { printf(\’fopen 文件错误\\n\’); \’%d\’, temp); cpu_temperture.integer_part=temp % 1000/100; (fp); return cpu_temperture;}int main(int arc, char *argv[]){ cpu_temperture_t cpu_temperture={0}; CPU_TEMP_FILE0); cpu_temperture.integer_part, cpu_temperture.decmal_part);

0d052b94507e42bc83fe26208fc2f2c1~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=Pn%2BcYNUhOc7C6jMXmDD8Pk6xetI%3D

获取文件大小

例如,如果需要发送文件的内容,则可能需要获取文件的大小。

关于嵌入式物联网,有很多东西需要学习。如果你不学错路线或者内容,你的工资就会增加。

免费共享约150GB的数据包。学习内容、访谈、项目都比较新、广泛。据估计,在网上购买某些鱼至少要花费几十美元。

点此寻找助手,0元领取:嵌入式物联网学习资料(今日头条)

624a083fc43b4793b92d2852c9cc8b93~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=2pHLJbTO1znowEHmnNm3N1c7wbQ%3D f2620d0bc9e74b8ab97cf82f7b9dccc8~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=yaZKSHzKW%2Fd8bxRxmpf0DlwnMoc%3D

代码:

向左或向右滑动即可查看所有代码

#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错误\\n\’); } fseek(fp, 0L, SEEK_END); 返回大小;}int main(){ #define FILE_NAME \’ long file_size=get_file_size(FILE_NAME);执行结果:

941c8da98e594da19d3e25f9b6b18280~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=qlTwOrp%2FbA%2FiLo%2F%2FUpT%2B%2Bz%2B0gzk%3D

获取时间戳

例如,将时间戳数据附加到日志输出可以使分析更加容易。

代码:

向左或向右滑动即可查看所有代码

#include stdio.h #include unistd.h#include stdlib.h#include string.h#include errno.h#include sys/time.h#include time.hlong get_sys_time_ms(void){ long long time_ms=0; gettimeofday(sys_current_time, NULL); time_ms=((long long)sys_current_time.tv_sec*1000000 + sys_current_time.tv_usec)/1000;}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);

03c05901de9e4083898ee6d298d54cf7~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=boxIYUx6eh2FKpwkZqSbwAmlRTg%3D

获取MAC

MAC 地址可用作设备ID 来唯一标识设备。

代码:

向左或向右滑动即可查看所有代码

#include stdio.h#include net/if.h#include sys/ioctl.h#include arpa/inet.h#include unistd.h#include string.hint get_netif_mac(const char *_ifr_name, uint8_t *_mac){ int32_t ret=-1; int32_t sock=套接字(AF_INET, SOCK_STREAM, 0); if (套接字错误\\r\\n\’) } ifr_name, _ifr_name); (\’ioctl err:%d\\r\\n\’,ret) } 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[5]); 返回0;err: 返回-1;}int main(int argc, char **argv){ char mac_str[32]={0}; mac_str); \’mac=%s\\n\’, mac_str);

19fe7b7150be465eaf323d1a49231bf6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=tIphYXDomxsHKoHGGuXX9r7E58g%3D

获取IP

您可能需要获取您的本地IP以供显示。

代码:

向左或向右滑动即可查看所有代码

#include stdio.h#include net/if.h#include sys/ioctl.h#include arpa/inet.h#include unistd.h#include string.hint get_local_ip(const char *_ifr_name, char *_ip){ int ret=-1; struct sockfd=socket (-1==sockfd); } (ifr.ifr_name, IFNAMSIZ); ifr.ifr_name[IFNAMSIZ – 1]=0; ifr) 0) { printf(\’ioctl 错误\\n\’); } memcpy(sin, ifr.ifr_addr, sizeof(sin)); int ip_len=snprintf(_ip, \’%s\’, inet_ntoa(sin.sin_addr); )); }int main(int argc, char **argv){ char ip_str[32]={0}; get_local_ip(\’wlan1\’, ip_str);

f2f8fe9edaac434caa76f16aead8faef~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1718267521&x-signature=4EowUqXBz6HcWMyksOQRF4yt19U%3D 以上是本次分享的一些小代码片段。

如果您觉得这篇文章有帮助,请帮助我们转发。谢谢。

本文来自Embedded Hodgepodge,作者为Hodgepodge先生。

本文转载自《嵌入式与Linux》。如有侵权,请联系我们删除。

原始链接:分享一些工作代码片段(带有代码示例)

版权声明:本文来自网络,知识免费传授。版权归原作者所有。若作品存在版权问题,请联系我们删除。

版权声明:本文转载于网络,版权归作者所有。如有侵权,请联系本站编辑删除。

关于作者: 头条共创

热门文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注