使用CString成员读取类对象时出现错误指针错误 [英] Bad Pointer Error when I read a class object with CString member

查看:181
本文介绍了使用CString成员读取类对象时出现错误指针错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

课堂测试

{

public:

CString str;

}

test oTest;

oTest.str =abc;



当我尝试将上述对象'oTest'写入a使用fwrite和CFile的简单文件,然后尝试使用fread读取,我在oTest.str中得到坏指针



但是当我用char数组替换CString成员时char str [50]并尝试编写和读取文件,它的工作原理



所以我想知道如何使用CString成员编写和读取类对象并从一个文件





感谢您的快速解决方案。

我通过序列化完成它。但我想创建一种没有序列化的替代方法。

我想发送类似的结构或类与CString成员通过CAsyncSocket发送和接收::接收/发送

我能够发送类对象但无法在客户端应用程序中接收它。我在CString成员中得到一个错误的指针。而当我用一个char数组成员替换CString成员时,我能够将类对象作为数据包发送,并使用套接字在客户端中接收它。



我可以从上面的解决方案中得出结论,序列化是用CString成员在文件/插槽中读取/写入类对象的唯一方法

请确认

解决方案

CString 是一个对象,以及将其写入文件的正确方法是序列化。在我看来, MFC 支持序列化。请查看此CodeProject文章:序列化入门 - 第1部分 [< a href =http://www.codeproject.com/Articles/1843/A-serialization-primer-Parttarget =_ blanktitle =New Window> ^ ]。


这是因为 fwrite 不理解CString的结构,只会写入对象及其指针。属于该对象的实际数据将留在某处的内存中,而不是写入该文件。如果你想保存和恢复MFC对象,那么你应该使用 CArchive


写下长度CString :: GetLength()然后在 CString 中写入char数组。您可以通过将 CString 转换为 LPCTSTR 来访问空终止数组。



 CString str; 
int len = str.GetLength()+ 1;
fwrite(( const void *)(& len), sizeof int ), 1 ,fp); // 写长度
fwrite(( const void *)(LPCTSTR)str, sizeof (TCHAR),len,fp) ;





注意: CString :: GetLength()不包括 NULL 最后是char,所以如果你想编写NULL char,你必须添加一个。



读取时,读取长度1st,然后读取char数组。不要阅读超过长度!由于数组是空终止的,你可以这样做。



  int  len =  0 ; 
fread(( void *)(& len), sizeof int ), 1 ,fp); // 读取长度
TCHAR * pbuf = TCHAR [len];
fread(( void *)(pbuf), sizeof (TCHAR),len,fp ); // 读取char数组
CString str = pbuf; // 因为pbuf为空终止
delete [] pbuf;
pbuf = NULL;





或者你可以通过调用 CString ::来避免额外的内存分配GetBuffer



  int  len =  0 ; 
fread(( void *)(& len), sizeof int ), 1 ,fp); // 读取长度
CString str;
LPTSTR * pbuf = str.GetBuffer(len);
fread(( void *)(pbuf), sizeof (TCHAR),len,fp ); // 读取char数组
str.ReleaseBuffer();


class test
{
public:
CString str;
}
test oTest;
oTest.str = "abc";

When I try to write the above object 'oTest' to a simple file using fwrite and CFile and then try to read using fread, I am getting bad pointer in oTest.str

But when I replace the CString member with char array as "char str[50]" and try to write and read file, its working

So I want to know how to write and read a class object with CString member to and from a file


Thanks for the quick solution.
I have it done throught serialization. but I thought of creating a alternate way without serialization.
I would like to send the similar struture or class with CString member to be send and receive via CAsyncSocket::Receive/Send
I am able to send the class object but unable to receive it in the client application. I am getting a bad pointer in the CString member. whereas when I replace the CString member with a char array member I am able to send the class object as a packet and receive the same in the client using sockets.

Can I conclude from your above solution that serialization is the only way to read/write a class object with CString member to and from file/socket
Please confirm

解决方案

CString is an object, and the proper way to write it to a file is serialization. MFC supports serialization very well, in my opinion. Have a look at this CodeProject article: "A serialization primer - Part 1"[^].


That is because the fwrite does not understand a CString's structure and will just write the object and its pointer(s). The actual data belonging to the object will be left in memory somewhere and not written to the file. If you wish to save and restore MFC objects then you should use a CArchive.


Write the length with CString::GetLength() and then write the char array in CString. You can access the null terminated array by casting CString to LPCTSTR.

CString str;
int len=str.GetLength()+1;
fwrite((const void*)(&len), sizeof(int), 1, fp); // write the length
fwrite((const void*)(LPCTSTR)str, sizeof(TCHAR), len, fp);



Note: CString::GetLength() does not include the NULL char at the end, so you have to add one if you want to write the NULL char as well.

When reading, read the length 1st and then the char array. Do not read more than the length! Since array is null terminated, you can just do this.

int len=0;
fread((void*)(&len), sizeof(int), 1, fp); // read the length
TCHAR* pbuf = new TCHAR[len];
fread((void*)(pbuf), sizeof(TCHAR), len, fp); // read the char array
CString str = pbuf; // since pbuf is null terminated
delete [] pbuf;
pbuf = NULL;



Or you can avoid the extra memory allocation by calling CString::GetBuffer.

int len=0;
fread((void*)(&len), sizeof(int), 1, fp); // read the length
CString str;
LPTSTR* pbuf = str.GetBuffer(len);
fread((void*)(pbuf), sizeof(TCHAR), len, fp); // read the char array
str.ReleaseBuffer();


这篇关于使用CString成员读取类对象时出现错误指针错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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