目的:申请一片缓冲,将结构体内容传递进入该缓冲再读取出来。
数据:结构体一成员为指针类型,结构体二成员为非指针类型。
用到的函数:
calloc();
malloc();
memcpy();
strcpy();
sprintf();将整形或者无符号整形格式化输入进入一个字符串。
atoi();字符串转int类型。
stat();等。
代码:
一:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 1000
#pragma pack (push, 1)
typedef struct Data_Package_Head
{
unsigned short *Version;
unsigned int *data_len;
char *pack_name;
}PACK_HEAD;
#pragma pack (pop)
int main(int argc,char **argv)
{
char *buf;
PACK_HEAD *pack_head;
char *src = "Add some words and a struct of Head.\n";
buf = ( char *)calloc (BUFFSIZE, sizeof (char)); //char
pack_head = (PACK_HEAD *)malloc (sizeof (PACK_HEAD));
pack_head -> pack_name = (char *)malloc (37); //convert to char
pack_head -> Version = (unsigned short *)malloc (sizeof (unsigned short));
pack_head -> data_len = (unsigned int *)malloc (sizeof (unsigned int));
memcpy (pack_head->pack_name, src, 37);
memcpy(buf, pack_head->pack_name, 37);
buf += 37;
unsigned short vv = 123;
pack_head->Version = &vv;
sprintf (buf, "%hu", *(pack_head->Version));//key! 3B in buf if vv is 123!
buf += 2;
unsigned int dl = 12345678; //Set 8 bits always.Now the mem are 47B.
pack_head -> data_len = &dl;
sprintf (buf, "%u", *(pack_head->data_len));
printf ("dl3=%c\n",buf[2]);
buf -= 39;
printf ("%c\n",buf[0]);
printf ("head:%s",pack_head->pack_name);
printf ("buf:%s\n",buf);
PACK_HEAD *pack_head_2;
pack_head_2 = (PACK_HEAD *)malloc (sizeof (PACK_HEAD));
pack_head_2 -> pack_name = (char *)malloc (37);
pack_head_2 -> Version = (unsigned short *)malloc (sizeof (unsigned short));
memcpy(pack_head_2->pack_name, buf,37);
unsigned short vv_2;
//vv_2 = (((unsigned short)buf[37])-48)*10 + ((unsigned short)buf[38]-48);Y
char tmp_Vrs[2];
tmp_Vrs[0] = buf[37];
tmp_Vrs[1] = buf[38];
vv_2 = atoi (tmp_Vrs);
printf ("vv_2=%hu\n",vv_2);
pack_head_2 -> Version = &vv_2;
printf("%s\n",pack_head_2->pack_name);
printf ("ph_2_Vrs=%hu",*(pack_head_2 -> Version));
free(buf);
free(pack_head);
free(pack_head_2);
pack_head_2 = NULL;
buf=NULL;
pack_head=NULL;
return 0;
}
二:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#define BUFFSIZE 1000
#define XXX_YYY_VERSION 0xff
#pragma pack (push, 1)
typedef struct Data_Package_Head
{
int version;
int data_len;
char pack_name[128];
}PACK_HEAD;
#pragma pack (pop)
int main(int argc, char **argv)
{
PACK_HEAD pack_head;
char buff[BUFFSIZE] = {0};
/*file stat*/
struct stat buff_stat;
int file_size;
FILE *fd = fopen (argv[1], "rb");
if (fd == NULL){
perror ("fopen fail");
exit (1);
}else{
if (stat (argv[1], &buff_stat) == -1){
perror ("stat fail");
exit (1);
}else
file_size = buff_stat.st_size; //The size of the file.
}
printf ("fd_sz:%d\n", file_size);
strcpy (pack_head.pack_name, "xxxxxxxxxxxxxxxxxxxxx.");
pack_head.version = DSPPA_YIXIANTONG_VERSION;
pack_head.data_len = file_size;
int pack_head_len = sizeof(PACK_HEAD);
printf ("head_len:%d\n",pack_head_len);
//int data_len = sizeof(src);
memcpy (buff, &pack_head, pack_head_len);
printf ("%s\n",buff);
printf ("buff..:%c\n",buff[5]);
PACK_HEAD pack_head_2;
memcpy (&pack_head_2, buff, pack_head_len); //to struct 2
printf ("ph2.version:%d\n", pack_head_2.version);
printf ("ph2.dl:%d\n", pack_head_2.data_len);
printf ("ph2.pkname:%s\n",pack_head_2.pack_name);
return 0;
}
小结:
01. 对c语言当中对内存的写入和管理有一定理解了;
02. 很多小细节,包括格式化输出、数据类型转化以及一些函数参数的理解。
03. 结构体字节对齐理解加深,包括以1字节对齐方法。
04. 多使用参数化方式,接口化方式,有待改进。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有