Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Updated 8-Jun-15 22:14pm
v2
Comments
Jochen Arndt 9-Jun-15 2:55am    
You should show us the code where the error occurs (probably when reading from file).
Provide also if Unicode is set in your project settings (then CString uses wide characters).
Richard MacCutchan 9-Jun-15 5:55am    
Yes, serialization is the only way. These are objects you are dealing with, not simple character arrays.

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"[^].
 
Share this answer
 
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.
 
Share this answer
 
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.

C++
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.

C++
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.

C++
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900