0

データを構造体から文字配列にコピーしようとしているときに、奇妙な問題に直面しています。これは、ソケットを介してサーバーに送信されます。以下は参考のための私のコードです

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

}

文字配列の内容を印刷しようとすると、何も表示されません。どこが間違っているのか教えてください。どんな助けでも大歓迎です

4

2 に答える 2

1

配列の内容をダンプしたい場合は、printf書き込む文字数を指定する必要があります。そうしないと、最初の 0 バイトで停止します。

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

これには印刷できない文字が含まれているため、16 進ダンプをお勧めします:構造データの 16 進ダンプを取得する方法

于 2012-07-31T11:10:21.253 に答える
1

printf()(そして、char文字列を操作する他のすべての関数は、文字列が 0 バイトに遭遇したときに終了すると仮定します。構造体の最初のバイトは実際には 0 であるため、バッファを出力しても何も出力されません。さらに、ゼロ、その他の文字のほとんどは同様に印刷できない制御文字であり、画面には表示されません。

検査できるようにバッファを画面に表示したい場合は、おそらく文字をループして、それぞれを 16 進数のバイトとして出力する必要があります。

このアプローチには注意してください。配列を操作するために使用する他の関数charが誤ってゼロで終了しないようにしてください。

于 2012-07-31T11:06:41.380 に答える