Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将C结构复制到字符数组_C - Fatal编程技术网

将C结构复制到字符数组

将C结构复制到字符数组,c,C,在尝试将数据从结构复制到字符数组,然后通过套接字发送到服务器时,我遇到了一个奇怪的问题。以下是我的代码供您参考 #include <stdio.h> #define MAX_MSG_SIZE 1024 #pragma pack(1) struct msgheader{ unsigned int messageLength; unsigned int messageType; }; struct floating_field{ unsigned char tag; unsigned c

在尝试将数据从结构复制到字符数组,然后通过套接字发送到服务器时,我遇到了一个奇怪的问题。以下是我的代码供您参考

#include <stdio.h>
#define MAX_MSG_SIZE 1024
#pragma pack(1)
struct msgheader{
unsigned int messageLength;
unsigned int messageType;
};
struct floating_field{
unsigned char tag;
unsigned char length;
};
struct open_req{
    struct msgheader mhdr;
unsigned int invokeid;
unsigned int version;
unsigned int timeout;
unsigned int peripheralid;
unsigned int servicesrequested;
unsigned int callmsgmask;
unsigned int agentstatemask;
unsigned int configmsgmask;
unsigned int reserved_1;
unsigned int reserved_2;
unsigned int reserved_3;
};
char uccebuf[MAX_MSG_SIZE];
unsigned int invokeid = 1;
char clientid[256];
char clientpassword[256];
int main(int argc, char *argv[]);
void func1();
int main(int argc, char *argv[]){
func1();
exit(0);
}
void func1(){
int retval, error, bytes;
struct open_req *request;
struct floating_field *ff;
char *p = uccebuf;
unsigned int total_bytes, result;
int templen,cnt;
cnt = 0;
//socklen_t len;
error = 0;
//request = (struct open_req *)malloc(sizeof(struct open_req));
//ff = (struct ffloating_field *)malloc(sizeof(struct floating_field));
strcpy(clientid,"admin");
strcpy(clientpassword,"12345");
request = (struct open_req *)uccebuf;
total_bytes = 0;
request->mhdr.messageType = 3;
//memcpy(&uccebuf[cnt], &request->mhdr.messageType, sizeof(request->mhdr.messageType));
request->invokeid = invokeid++;
request->version = 13;
request->timeout = 0xFFFFFFFF;
request->peripheralid = 0xFFFFFFFF;
request->servicesrequested = 0x00000010;
request->callmsgmask = 0x00000001 | 0x00000002 | 0x00000004 | 0x00000020 | 0x00000200 | 0x00000100 | 0x00040000 | 0x00000400 | 0x00010000;
request->agentstatemask = 0x00000000;
request->configmsgmask = 0x00000000;
request->reserved_1 = 0x00000000;
request->reserved_2 = 0x00000000;
request->reserved_3 = 0x00000000;
//memcpy(uccebuf,&request,sizeof(struct open_req));
printf("request->peripheralid: %u\n", request->peripheralid);
printf("request->callmsgmask: %u\n", request->callmsgmask);
p = p + sizeof(struct open_req);
total_bytes += sizeof(struct open_req);
ff=(struct floating_field *)p;
ff->tag = 1;
templen = strlen(clientid);
ff->length = templen;
//memset(uccebuf,&ff,sizeof(struct floating_field));
total_bytes +=sizeof(struct floating_field);
p = p + sizeof(struct floating_field);
strcpy(p, clientid);
total_bytes += ff->length;
p = p + ff->length;
ff=(struct floating_field *)p;
ff->tag = 2;
templen = strlen(clientpassword);
ff->length = templen;
total_bytes +=sizeof(struct floating_field);
p = p + sizeof(struct floating_field);
strcpy(p, clientpassword);
total_bytes += strlen(clientpassword);
//memset(uccebuf,&ff,sizeof(struct floating_field));
request->mhdr.messageLength = (total_bytes - sizeof(struct msgheader));
printf("\nMessage to be send is: %s", uccebuf);
#包括
#定义最大消息大小1024
#布拉格语包(1)
结构msgheader{
无符号整数消息长度;
无符号int-messageType;
};
结构浮动字段{
无符号字符标签;
无符号字符长度;
};
结构打开请求{
结构msgheader mhdr;
无符号整数;
无符号整数版本;
无符号整数超时;
无符号int外设;
请求的未签名整数服务;
无符号整数callmsgmask;
未签名的int-agentstatesmask;
无符号整数配置msgmask;
无符号整数保留_1;
无符号整数保留_2;
无符号整数保留_3;
};
字符uccebuf[最大字符大小];
无符号整数invokeid=1;
char-clientid[256];
char clientpassword[256];
intmain(intargc,char*argv[]);
void func1();
int main(int argc,char*argv[]){
func1();
出口(0);
}
void func1(){
int retval,错误,字节;
结构打开请求*;
结构浮动_字段*ff;
char*p=uccebuf;
无符号整数总字节数,结果;
int templen,cnt;
cnt=0;
//索克伦;
误差=0;
//请求=(struct open_req*)malloc(sizeof(struct open_req));
//ff=(结构浮动字段*)malloc(sizeof(结构浮动字段));
strcpy(clientid,“admin”);
strcpy(客户端密码,“12345”);
请求=(结构打开_请求*)uccebuf;
总字节数=0;
请求->mhdr.messageType=3;
//memcpy(&uccebuf[cnt],&request->mhdr.messageType,sizeof(request->mhdr.messageType));
请求->invokeid=invokeid++;
请求->版本=13;
请求->超时=0xFFFFFF;
请求->外设ID=0xFFFFFFFF;
请求->服务请求=0x00000010;
请求->调用MSGMask=0x00000001 | 0x00000002 | 0x00000004 | 0x00000020 | 0x00000200 | 0x00000100 | 0x00040000 | 0x00000400 | 0x0001000000;
请求->代理状态掩码=0x00000000;
请求->配置MSGMask=0x00000000;
请求->保留\u 1=0x00000000;
请求->保留_2=0x00000000;
请求->保留\u 3=0x00000000;
//memcpy(uccebuf,&request,sizeof(struct open_req));
printf(“请求->外围设备ID:%u\n”,请求->外围设备ID);
printf(“请求->调用msgmask:%u\n”,请求->调用msgmask);
p=p+sizeof(结构开放需求);
总字节数+=sizeof(结构打开请求);
ff=(结构浮动_字段*)p;
ff->tag=1;
templen=strlen(clientid);
ff->长度=模板;
//memset(uccebuf,&ff,sizeof(struct floating_字段));
总字节数+=sizeof(结构浮动字段);
p=p+sizeof(结构浮动字段);
strcpy(p,clientid);
总字节数+=ff->长度;
p=p+ff->长度;
ff=(结构浮动_字段*)p;
ff->tag=2;
templen=strlen(客户端密码);
ff->长度=模板;
总字节数+=sizeof(结构浮动字段);
p=p+sizeof(结构浮动字段);
strcpy(p,clientpassword);
总字节数+=strlen(客户端密码);
//memset(uccebuf,&ff,sizeof(struct floating_字段));
请求->mhdr.messageLength=(总字节数-sizeof(struct msgheader));
printf(“\n要发送的消息是:%s”,uccebuf);
}

当我试图打印字符数组的内容时,它不会显示任何内容。你能告诉我哪里出了问题吗?非常感谢您的帮助

printf()
(以及在
char
字符串上运行的所有其他函数,假设遇到零字节时字符串终止。实际上,结构的第一个字节是0,因此打印缓冲区将不会打印任何内容。此外,除了零之外,大多数其他字符也是不可打印的控制字符,并且dn不会出现在屏幕上

如果您想在屏幕上呈现缓冲区以便检查它,您可能应该循环字符并将每个字符打印为十六进制字节


使用这种方法时要小心;确保用于操作
char
数组的其他函数都不会意外地被零终止。

如果要转储数组的内容,需要告诉
printf
要写入多少个字符,否则它将在第一个零字节处停止:

printf("\nMessage to be send is: %.*s", (int) sizeof(open_req), uccebuf);

这将包含不可打印的字符,因此我推荐一个十六进制转储:

请缩进代码以使其可读,并考虑如果一个更小的版本可能出现问题。为什么要复制它们?不能使用打包的联合吗?