首页 > 社交 > 科普中国

分享几个实用的代码片段(第二弹)

常驻编辑 科普中国 2022-09-22 片段   代码   杂烩   嵌入式   大杂烩   公众   大小   成员   结构   文件

大家好,我是杂烩君。6Aw拜客生活常识网

本次我们再来分享几个实用的代码小片段。6Aw拜客生活常识网

快速获取结构体成员大小

获取结构体成员大小及偏移量的方式有多种。最简便的方式:6Aw拜客生活常识网

代码:6Aw拜客生活常识网

左右滑动查看全部代码>>>6Aw拜客生活常识网

// 微信公众号:嵌入式大杂烩
#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
", 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;
}

运行结果:6Aw拜客生活常识网

6Aw拜客生活常识网

文件操作

文件操作平时用得很多,为了方便使用,可以自己根据实际需要再封装一层:6Aw拜客生活常识网

代码:6Aw拜客生活常识网

左右滑动查看全部代码>>>6Aw拜客生活常识网

// 微信公众号:嵌入式大杂烩
#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!
", 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;
}
    

相关阅读:

  • 《安乐传》是改编的哪个小说?有吻戏片段吗?
  • 适合女孩子发的朋友圈日常文案
  • “片段睡眠”要不得,提倡晚上11时前睡觉
  • 盘点近乎满分的“神剧”(二)丨“她,挺有脾气的,不是吗?”
  • 没有一个人甘愿平庸,送给每一个为了自己的未来努力奋斗
  • 若无闲事挂心头,便是读书好时节
  • 《大汉天子》播出二十周年 黄晓明分享片段回忆刘彻
  • 温暖的等待
  • 抖音怎么剪辑音乐?
  • 《雷神4》发布新片段:女雷神喵喵锤变成暴雨梨花钉
    • 网站地图 |
    • 声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。文章内容仅供参考,不做权威认证,如若验证其真实性,请咨询相关权威专业人士。