【问题标题】:Copying C Structures to a character array将 C 结构复制到字符数组
【发布时间】:2012-07-31 11:04:06
【问题描述】:

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

#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);

}

当我尝试打印字符数组的内容时,它没有显示任何内容。你能告诉我哪里出错了吗?任何帮助都非常感谢

【问题讨论】:

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

标签: c


【解决方案1】:

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

如果你想在屏幕上渲染缓冲区以便检查它,你可能应该遍历字符并将每个字符打印为十六进制字节。

小心这种方法;确保您用来操作 char 数组的其他函数不会被零意外终止。

【讨论】:

    【解决方案2】:

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

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

    这将包含不可打印的字符,所以我建议使用十六进制转储:how to get hexdump of a structure data

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 2015-01-06
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多