在线不卡日本ⅴ一区v二区_精品一区二区中文字幕_天堂v在线视频_亚洲五月天婷婷中文网站

  • <menu id="lky3g"></menu>
  • <style id="lky3g"></style>
    <pre id="lky3g"><tt id="lky3g"></tt></pre>

    分享幾個(gè)實(shí)用的代碼片段(第二彈)

    大家好,我是雜燴君。

    本次我們?cè)賮?lái)分享幾個(gè)實(shí)用的代碼小片段。

    快速獲取結(jié)構(gòu)體成員大小

    獲取結(jié)構(gòu)體成員大小及偏移量的方式有多種。最簡(jiǎn)便的方式:

    代碼:

    左右滑動(dòng)查看全部代碼>>>

    // 微信公眾號(hào):嵌入式大雜燴#include// 獲取結(jié)構(gòu)體成員大小#defineGET_MEMBER_SIZE(type, member) sizeof(((type*)0)->member)// 獲取結(jié)構(gòu)體成員偏移量#defineGET_MEMBER_OFFSET(type, member)((size_t)(&(((type*)0)->member)))typedef struct _test_struct0{ char x; char y;char z;}test_struct0;typedef struct _test_struct1{ char a; char c;short b;int d; test_struct0 e;}test_struct1;int main(int arc, char *argv[]){ printf(“GET_MEMBER_SIZE(test_struct1, a) = %ld”, GET_MEMBER_SIZE(test_struct1, a));printf(“GET_MEMBER_SIZE(test_struct1, c) = %ld”, GET_MEMBER_SIZE(test_struct1, c)); printf(“GET_MEMBER_SIZE(test_struct1, b) = %ld”, GET_MEMBER_SIZE(test_struct1, b)); printf(“GET_MEMBER_SIZE(test_struct1, d) = %ld”, GET_MEMBER_SIZE(test_struct1, d));printf(“GET_MEMBER_SIZE(test_struct1, e) = %ld”, GET_MEMBER_SIZE(test_struct1, e));printf(“test_struct1 size = %ld”, sizeof(test_struct1)); printf(“GET_MEMBER_OFFSET(a): %ld”, GET_MEMBER_OFFSET(test_struct1, a)); printf(“GET_MEMBER_OFFSET(c): %ld”, GET_MEMBER_OFFSET(test_struct1, c)); printf(“GET_MEMBER_OFFSET(b): %ld”, GET_MEMBER_OFFSET(test_struct1, b)); printf(“GET_MEMBER_OFFSET(d): %ld”, GET_MEMBER_OFFSET(test_struct1, d)); printf(“GET_MEMBER_OFFSET(e): %ld”, GET_MEMBER_OFFSET(test_struct1, e)); return 0;}

    運(yùn)行結(jié)果:

    文件操作

    文件操作平時(shí)用得很多,為了方便使用,可以自己根據(jù)實(shí)際需要再封裝一層:

    代碼:

    左右滑動(dòng)查看全部代碼>>>

    // 微信公眾號(hào):嵌入式大雜燴#includestatic int file_opt_write(const char *filename, void *ptr, int size){ FILE *fp;size_t num;fp = fopen(filename, “wb”);if(NULL == fp){printf(“open %s file error!”, filename);return -1; }num = fwrite(ptr, 1, size, fp);if(num != size){fclose(fp);printf(“write %s file error!”, filename);return -1;} fclose(fp);return num;}static int file_opt_read(const char *filename, void *ptr, int size){FILE *fp;size_t num;fp = fopen(filename, “rb”);if(NULL == fp){printf(“open %s file error!”, filename);return -1;}num = fread(ptr, 1, size, fp);if(num != size){fclose(fp);printf(“write %s file error!”, filename);return -1;} fclose(fp);return num;}typedef struct _test_struct{ char a; char c;short b;int d;}test_struct;int main(int arc, char *argv[]){#define FILE_NAME”./test_file”test_struct write_data = {0};write_data.a = 1;write_data.b = 2;write_data.c = 3;write_data.d = 4;printf(“write_data.a = %d”, write_data.a);printf(“write_data.b = %d”, write_data.b);printf(“write_data.c = %d”, write_data.c);printf(“write_data.d = %d”, write_data.d);file_opt_write(FILE_NAME, (test_struct*)&write_data, sizeof(test_struct));test_struct read_data = {0};file_opt_read(FILE_NAME, (test_struct*)&read_data, sizeof(test_struct));printf(“read_data.a = %d”, read_data.a);printf(“read_data.b = %d”, read_data.b);printf(“read_data.c = %d”, read_data.c);printf(“read_data.d = %d”, read_data.d); return 0;}

    運(yùn)行結(jié)果:

    嵌入式物聯(lián)網(wǎng)需要學(xué)的東西真的非常多,千萬(wàn)不要學(xué)錯(cuò)了路線和內(nèi)容,導(dǎo)致工資要不上去!

    無(wú)償分享大家一個(gè)資料包,差不多150多G。里面學(xué)習(xí)內(nèi)容、面經(jīng)、項(xiàng)目都比較新也比較全!某魚(yú)上買(mǎi)估計(jì)至少要好幾十。

    點(diǎn)擊這里找小助理0元領(lǐng)?。呵度胧轿锫?lián)網(wǎng)學(xué)習(xí)資料(頭條)

    進(jìn)度條

    有時(shí)候,加上進(jìn)度條可以比較方便知道當(dāng)前的下載進(jìn)度、寫(xiě)入文件的進(jìn)度等。

    代碼:

    左右滑動(dòng)查看全部代碼>>>

    // 微信公眾號(hào):嵌入式大雜燴#include #include #include typedef struct _progress{int cur_size;int sum_size;}progress_t;void progress_bar(progress_t *progress_data){int percentage = 0;int cnt = 0;char proc[102];memset(proc, ”, sizeof(proc));percentage = (int)(progress_data->cur_size * 100 / progress_data->sum_size);printf(“percentage = %d %%”, percentage);if (percentage <= 100){while (cnt <= percentage){printf("[%-100s] [%d%%]r", proc, cnt);fflush(stdout);proc[cnt] = '#';usleep(100000);cnt++;}}printf("");}int main(int arc, char *argv[]){progress_t progress_test = {0};progress_test.cur_size = 65;progress_test.sum_size = 100;progress_bar(&progress_test);return 0;}

    運(yùn)行結(jié)果:

    日志輸出

    日志輸出常常需要帶一些格式。最簡(jiǎn)單的方式如:

    代碼:

    左右滑動(dòng)查看全部代碼>>>

    // 微信公眾號(hào):嵌入式大雜燴#include#define LOG_D(fmt, args…) do{printf(” “, __FILE__, __LINE__, __FUNCTION__);printf(fmt, ##args);}while(0)int main(int arc, char *argv[]){char ch = ‘a’;char str[10] = “ZhengN”;float float_val = 10.10;int num = 88;double double_val = 10.123456;LOG_D(“字符為 %c “, ch);LOG_D(“字符串為 %s ” , str);LOG_D(“浮點(diǎn)數(shù)為 %f “, float_val);LOG_D(“整數(shù)為 %d” , num);LOG_D(“雙精度值為 %lf “, double_val);LOG_D(“八進(jìn)制值為 %o “, num);LOG_D(“十六進(jìn)制值為 %x “, num); return 0;}

    運(yùn)行結(jié)果:

    可閱讀往期文章:

    C語(yǔ)言、嵌入式中幾個(gè)非常實(shí)用的宏技巧

    一個(gè)簡(jiǎn)單的日志模塊

    后臺(tái)運(yùn)行生成core文件

    這個(gè)是我們上一篇文章分享一種你可能不知道的bug定位方法介紹的,方便大家使用,也匯總在這里。

    代碼:

    左右滑動(dòng)查看全部代碼>>>

    // 微信公眾號(hào):嵌入式大雜燴#include #include #include #include #define SHELL_CMD_CONF_CORE_FILE”echo /var/core-%e-%p-%t > /proc/sys/kernel/core_pattern”#define SHELL_CMD_DEL_CORE_FILE “rm -f /var/core*”static int enable_core_dump(void){int ret = -1;int resource = RLIMIT_CORE;struct rlimit rlim;rlim.rlim_cur = 1 ? RLIM_INFINITY : 0;rlim.rlim_max = 1 ? RLIM_INFINITY : 0;system(SHELL_CMD_DEL_CORE_FILE);if (0 != setrlimit(resource, &rlim)){printf(“setrlimit error!”);return -1;}else{system(SHELL_CMD_CONF_CORE_FILE);printf(“SHELL_CMD_CONF_CORE_FILE”);return 0;}return ret;}int main(int argc, char **argv){enable_core_dump();printf(“==================segmentation fault test==================”);int *p = NULL;*p = 1234;return 0;}

    以上就是本次分享的幾個(gè)小的代碼片段。

    期待你的三連支持!

    原文作者:雜燴君

    作品來(lái)源:嵌入式大雜燴

    來(lái)源鏈接:https://mp.weixin.qq.com/s/_YIlvFdVYPLAUkyGsn3u3g

    鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場(chǎng),版權(quán)歸原作者所有,如有侵權(quán)請(qǐng)聯(lián)系管理員(admin#wlmqw.com)刪除。
    上一篇 2022年9月22日 06:11
    下一篇 2022年9月22日 06:12

    相關(guān)推薦

    聯(lián)系我們

    聯(lián)系郵箱:admin#wlmqw.com
    工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息