www.久久久久|狼友网站av天堂|精品国产无码a片|一级av色欲av|91在线播放视频|亚洲无码主播在线|国产精品草久在线|明星AV网站在线|污污内射久久一区|婷婷综合视频网站

當前位置:首頁 > 嵌入式 > 嵌入式大雜燴
[導讀]文件操作平時用得很多,為了方便使用,可以自己根據(jù)實際需要再封裝一層:

#include // 獲取結構體成員大小 #define GET_MEMBER_SIZE(type, member)   sizeof(((type*)0)->member) // 獲取結構體成員偏移量 #define GET_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\n", GET_MEMBER_SIZE(test_struct1, a)); printf("GET_MEMBER_SIZE(test_struct1, c) = %ld\n", GET_MEMBER_SIZE(test_struct1, c)); printf("GET_MEMBER_SIZE(test_struct1, b) = %ld\n", GET_MEMBER_SIZE(test_struct1, b)); printf("GET_MEMBER_SIZE(test_struct1, d) = %ld\n", GET_MEMBER_SIZE(test_struct1, d)); printf("GET_MEMBER_SIZE(test_struct1, e) = %ld\n", GET_MEMBER_SIZE(test_struct1, e)); printf("test_struct1 size = %ld\n", sizeof(test_struct1)); printf("GET_MEMBER_OFFSET(a): %ld\n", GET_MEMBER_OFFSET(test_struct1, a)); printf("GET_MEMBER_OFFSET(c): %ld\n", GET_MEMBER_OFFSET(test_struct1, c)); printf("GET_MEMBER_OFFSET(b): %ld\n", GET_MEMBER_OFFSET(test_struct1, b)); printf("GET_MEMBER_OFFSET(d): %ld\n", GET_MEMBER_OFFSET(test_struct1, d)); printf("GET_MEMBER_OFFSET(e): %ld\n", GET_MEMBER_OFFSET(test_struct1, e)); return 0;
}

運行結果:

文件操作

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

代碼:

左右滑動查看全部代碼>>>

// 微信公眾號:嵌入式大雜燴 #include   static 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!\n", filename); return -1;   
    }
    
    num = fwrite(ptr, 1, size, fp); if(num != size)
    {
        fclose(fp); printf("write %s file error!\n", 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!\n", filename); return -1;
    }
    
    num = fread(ptr, 1, size, fp); if(num != size)
    {
        fclose(fp); printf("write %s file error!\n", 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\n", write_data.a); printf("write_data.b = %d\n", write_data.b); printf("write_data.c = %d\n", write_data.c); printf("write_data.d = %d\n", 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\n", read_data.a); printf("read_data.b = %d\n", read_data.b); printf("read_data.c = %d\n", read_data.c); printf("read_data.d = %d\n", read_data.d); return 0;
}

運行結果:

進度條

有時候,加上進度條可以比較方便知道當前的下載進度、寫入文件的進度等。

代碼:

左右滑動查看全部代碼>>>

// 微信公眾號:嵌入式大雜燴 #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, '\0', sizeof(proc));

    percentage = (int)(progress_data->cur_size * 100 / progress_data->sum_size); printf("percentage = %d %%\n", percentage); if (percentage <= 100)
    { while (cnt <= percentage) { printf("[%-100s] [%d%%]\r", proc, cnt);
            fflush(stdout);  
            proc[cnt] = '#';  
            usleep(100000);
            cnt++;
        }

    } printf("\n");
} 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;
}

運行結果:

日志輸出

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

代碼:

左右滑動查看全部代碼>>>

// 微信公眾號:嵌入式大雜燴 #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 \n", ch);
    LOG_D("字符串為 %s \n" , str);
    LOG_D("浮點數(shù)為 %f \n", float_val);
    LOG_D("整數(shù)為 %d\n" , num);
    LOG_D("雙精度值為 %lf \n", double_val);
    LOG_D("八進制值為 %o \n", num);
    LOG_D("十六進制值為 %x \n", num); return 0;
}

運行結果:

可閱讀

本站聲明: 本文章由作者或相關機構授權發(fā)布,目的在于傳遞更多信息,并不代表本站贊同其觀點,本站亦不保證或承諾內容真實性等。需要轉載請聯(lián)系該專欄作者,如若文章內容侵犯您的權益,請及時聯(lián)系本站刪除。
換一批
延伸閱讀

全球半導體封裝市場正向PLP、ECP等先進技術傾斜,以應對5G和高性能計算需求。但國內上規(guī)模的PLP廠商不超過五家,芯友微憑借技術創(chuàng)新和成本優(yōu)勢已占據(jù)一席之地。面對行業(yè)競爭和終端需求波動,張博威認為:“機會永遠都在,關鍵...

關鍵字: PLP ECP 封裝 芯友微 XINYOUNG

在半導體封裝領域,BGA(球柵陣列)封裝技術憑借其高引腳密度、低電阻電感和優(yōu)異散熱性能,已成為高性能芯片的主流封裝形式。然而,隨著芯片集成度與功率密度的持續(xù)提升,BGA焊點中的裂紋與微孔缺陷逐漸成為制約產品可靠性的核心問...

關鍵字: BGA裂紋 半導體 封裝

上海2025年8月20日 /美通社/ -- 今日,全球領先的集成電路成品制造和技術服務提供商長電科技(600584.SH)公布了2025年半年度報告。財報顯示,今年上半年長電...

關鍵字: 封裝 長電科技 系統(tǒng)集成 汽車電子

在嵌入式系統(tǒng)開發(fā)中,硬件抽象層(Hardware Abstraction Layer,HAL)起著至關重要的作用。它為上層軟件提供了統(tǒng)一的硬件訪問接口,隱藏了底層硬件的細節(jié),使得軟件具有更好的可移植性和可維護性。C++作...

關鍵字: 嵌入式C++ HAL 寄存器 封裝

在當今科技飛速發(fā)展的時代,半導體產業(yè)無疑是全球經濟和科技競爭的核心領域。隨著摩爾定律逐漸逼近物理極限,傳統(tǒng)的芯片制程微縮面臨著巨大挑戰(zhàn),而先進封裝技術卻異軍突起,成為推動半導體產業(yè)持續(xù)發(fā)展的新引擎。尤其是國產先進封裝技術...

關鍵字: 半導體 芯片 封裝

在IEDM2024上,英特爾代工的技術研究團隊展示了晶體管和封裝技術的開拓性進展,有助于滿足未來AI算力需求。

關鍵字: 晶體管 封裝 AI算力

由Manz亞智科技主辦,未來半導體協(xié)辦的“FOPLP、TGV、異質整合結構芯片封裝技術創(chuàng)新論壇”將于12月4日在蘇州尼盛萬麗酒店召開。本次論壇將以“‘化圓為方’解鎖高效封裝 ? CoPoS賦能芯未來”為主題,全面探討當前...

關鍵字: 封裝 半導體

一種集成FPGA(現(xiàn)場可編程門陣列)和DSP(數(shù)字信號處理器)芯粒的異構系統(tǒng)級封裝(SiP)是一種具有創(chuàng)新性和實用性的技術解決方案。以下是對這種異構系統(tǒng)級封裝的詳細解析:

關鍵字: FPGA DSP芯粒 封裝
關閉