如何将结构对象分配给char * [英] How to assign struct object into char *

查看:70
本文介绍了如何将结构对象分配给char *的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友,
现在我正在使用TCP/IP开发套接字程序.现在的问题是,我想从服务器和客户端传输结构数据,我想使用相同的结构接收相同的数据.

服务器端:
步骤1:使用CFile对象"pCustInfoFile" struct obj"oModeld传递以从文件读取daa.
步骤2:现在,我将此结构obj"oModel"分配给char * Buff.
步骤3:使用strncpy函数,将数据从oMdel复制到Buff.
步骤4:使用Socket obj"pSocket",将数据发送到客户端.

服务器端代码供您参考:

Hi friends,
Right now i am developing socket program using TCP/IP. Now problem is, I want to transmit structure data from server and in client, i want to receive same data using same structure.

Server Side:
Step1: Using CFile object "pCustInfoFile" struct obj "oModeld passed to read daa from file.
Step2: Now i Assign this struct obj "oModel" into char *Buff.
Step3: using strncpy function, copied data from oMdel in to Buff.
Step4: Using Socket obj "pSocket", the data sent to client side.

Server side code for your reference:

if(pCustInfoFile->Open(FName,CFile::modeRead))
{
   SModelInfo *oModel;  //"SModelInfo" is a structure
   char *Buff;
   oModel = new SModelInfo;
   pCustInfoFile->Read(oModel,sizeof(SModelInfo));
   
   strncpy(Buff,(char*)&oModel,sizeof(SModelInfo));
   pSocket->Send(Buff,sizeof(Buff));

}


客户端:

步骤1:使用大小和数据相同的char buff从客户端接收.
步骤2:oModel =(SModelInfo *)buff;
步骤3:如果将oModel->名称分配给sName(sName = oModel->名称)变量,则值(Name)不会得到


Client Side:

Step1: Using char buff with same size and data is receives in client side.
Step2: oModel = (SModelInfo*)buff;
Step3: If assigning oModel->Name to sName (sName = oModel->Name)variable then the value(Name) is not getting

int br=Receive(buff,sizeof(SModelInfo));
SModelInfo *oModel;
oModel = (SModelInfo*)buff;
CString sName;
sName = oModel->Name;


帮助我解决此问题.

谢谢与问候
S.Shanmuga Raja


Help me to fix this problem.

Thanks and Regards
S.Shanmuga Raja

推荐答案

在服务器端,您声明了Buff指针,但没有将其指向特定的内存.尝试为Buff分配内存.像这样的东西:

In the server side, you declared the Buff pointer but, didn''t point it to a specific memory. Try to allocate memory for Buff. Something like:

char *Buff = new char[sizeof(SModelInfo)];

而且,为什么要使用strncpy和指向SModelInfo的指针(您将oModel声明为指针...).尝试在堆栈中声明oModel.像这样的东西:

And, why do you use strncpy with the pointer to the pointer to SModelInfo (you declared oModel as a pointer...). Try to declare oModel in the stack instead. Something like:

SModelInfo oModel;
pCustInfoFile->Read(&oModel,sizeof(SModelInfo));

另一个问题:尝试更改Send函数以使用struct大小.像这样的东西:

Another issue: Try to change the Send function to use the struct size. Something like:

pSocket->Send(Buff,sizeof(SModelInfo));


在下面的代码中,您尝试将其从结构oModel复制到Buff,但是该指针尚未初始化,因此将损坏某些内存或导致内存访问错误.
In the following code you are trying to copy from the structure oModel to Buff, but that pointer has not been initialised so you will corrupt some memory or cause a memory access error.
char *Buff;
oModel = new SModelInfo;
pCustInfoFile->Read(oModel,sizeof(SModelInfo));

strncpy(Buff,(char*)&oModel,sizeof(SModelInfo));


它应该显示为


It should read

char *Buff;
oModel = new SModelInfo;
pCustInfoFile->Read(oModel,sizeof(SModelInfo));
Buff = new char[sizeof(SModelInfo)]; // allocate space for buffer
strncpy(Buff,(char*)oModel,sizeof(SModelInfo));



在下面的代码中,您将创建一个指针,并使用new对其进行初始化,然后立即使用其他指针覆盖它,从而丢失先前分配的块.



In the following code you create a pointer, initialise it with new and then immediately overwrite it with a different pointer thus losing the previously allocated block.

SModelInfo *oModel1;
oModel1 = new SModelInfo;
oModel1 = (SModelInfo*)buff;


应该是


This should be

SModelInfo *oModel1 = (SModelInfo*)buff; // just point at the existing buffer.


这篇关于如何将结构对象分配给char *的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆