To Read/Write data stored in a Structure to and from file.

Gurjeet Singh Batth's Avatar, Join Date: Nov 2007
Go4Expert Member
Hi EveryBody, I am again here with a petty issue................
Please Help me, I am a newly born programmer to MFC

To Write/Read structure on File, I wrote the following code.

Code:
/* test is a structure defined in stdafx.h*/
	/*
	struct test
	{
	CString name;
	CString age;
	};*/

Code:
void CFileOperationsDlg::OnBnClickedButton1()
{
	this->UpdateData (TRUE);
	CFile f;
	CFileException Fexcep;
	test buffWrite;
        test buffRead;
		
	buffWrite .age ="27";
	buffWrite .name = "My Name";

	f.Open ("E:\\MyFile.bcr", CFile ::modeCreate | CFile ::modeWrite  ,&Fexcep);
	f.Write (&buffWrite,sizeof(buffWrite ));
	f.Close ();
	
	if(f.Open("E:\\MyFile.bcr", CFile ::modeRead ,&Fexcep) == FALSE)
		return;
	try
        {
	        f.Read (&buffRead ,sizeof(buffRead ));
	}
	catch(CException *e)
	{
		e->AssertValid ();
	}
	f.Close ();
}
The Following Exception occurs when the last statement in code executes.........
Unhandled exception at 0x7c176d3f (mfc71d.dll) in FileOperations.exe: 0xC0000005: Access violation reading location 0xfeeefeee.
zahra_d84's Avatar, Join Date: Jan 2010
Newbie Member
I was faced with this problem.Who can help me make.
Gene Poole's Avatar, Join Date: Nov 2009
Contributor
CString is a class, not a plain old data (POD) type so it behaves differently. The MFC way to deal with this is serialization. All MFC classes derived from CObject support serialization. This is a means of taking all the typically hierarchical data stored in a class structure and converting it to a linear format more appropriate for storing in a stream. You can lookup MSDN help on serialization for more information.

Another approach is to convert the CString to POD types then store those in your file. Since CString is just a bunch of functions for string manipulation and a string buffer, a CString's complete state can be defined by the contents of it's buffer. The buffer is nothing more than a NULL terminated array of characters (either char or wchar_t depending on UNICODE settings--both POD types). You can go about this one of two ways: store an integral value representing the lenght of the string, then the characters themselves (this is sort-of how CString's internal Serialize() function works), or store all the characters and include the NULL terminator. There are advantages and disadvantages to both, so chose whichever is appropriate.

A simple implementation based on your code might look like this (added code in green):

Code:
void CFileOperationsDlg::OnBnClickedButton1()
{
	this->UpdateData (TRUE);
	CFile f;
	CFileException Fexcep;
	test buffWrite;
        test buffRead;
	size_t sz;
	TCHAR *buf;
		
	buffWrite .age ="27";
	buffWrite .name = "My Name";

	f.Open ("E:\\MyFile.bcr", CFile ::modeCreate | CFile ::modeWrite  ,&Fexcep);
	sz=buffWrite.GetLength();
	f.Write(&sz,sizeof(size_t));
	f.Write(buffWrite.GetBuffer(),sz*sizeof(TCHAR));
	//f.Write (&buffWrite,sizeof(buffWrite ));
	buffWrite.ReleaseBuffer();
	f.Close ();
	
	if(f.Open("E:\\MyFile.bcr", CFile ::modeRead ,&Fexcep) == FALSE)
		return;
	try
        {
	        f.Read(&sz,sizeof(size_t);
	        buf=new TCHAR[sz+1];
	        f.Read(buf,sz*sizeof(TCHAR));
	        buf[sz]=0;
	        buffRead.SetString(buf);
	        delete[] buf;
	        //f.Read (&buffRead ,sizeof(buffRead ));
	}
	catch(CException *e)
	{
		e->AssertValid ();
	}
	f.Close ();
}
zahra_d84's Avatar, Join Date: Jan 2010
Newbie Member
Quote:
Originally Posted by Gene Poole View Post
CString is a class, not a plain old data (POD) type so it behaves differently. The MFC way to deal with this is serialization. All MFC classes derived from CObject support serialization. This is a means of taking all the typically hierarchical data stored in a class structure and converting it to a linear format more appropriate for storing in a stream. You can lookup MSDN help on serialization for more information.

Another approach is to convert the CString to POD types then store those in your file. Since CString is just a bunch of functions for string manipulation and a string buffer, a CString's complete state can be defined by the contents of it's buffer. The buffer is nothing more than a NULL terminated array of characters (either char or wchar_t depending on UNICODE settings--both POD types). You can go about this one of two ways: store an integral value representing the lenght of the string, then the characters themselves (this is sort-of how CString's internal Serialize() function works), or store all the characters and include the NULL terminator. There are advantages and disadvantages to both, so chose whichever is appropriate.

A simple implementation based on your code might look like this (added code in green):

Code:
void CFileOperationsDlg::OnBnClickedButton1()
{
	this->UpdateData (TRUE);
	CFile f;
	CFileException Fexcep;
	test buffWrite;
        test buffRead;
	size_t sz;
	TCHAR *buf;
		
	buffWrite .age ="27";
	buffWrite .name = "My Name";

	f.Open ("E:\\MyFile.bcr", CFile ::modeCreate | CFile ::modeWrite  ,&Fexcep);
	sz=buffWrite.GetLength();
	f.Write(&sz,sizeof(size_t));
	f.Write(buffWrite.GetBuffer(),sz*sizeof(TCHAR));
	//f.Write (&buffWrite,sizeof(buffWrite ));
	buffWrite.ReleaseBuffer();
	f.Close ();
	
	if(f.Open("E:\\MyFile.bcr", CFile ::modeRead ,&Fexcep) == FALSE)
		return;
	try
        {
	        f.Read(&sz,sizeof(size_t);
	        buf=new TCHAR[sz+1];
	        f.Read(buf,sz*sizeof(TCHAR));
	        buf[sz]=0;
	        buffRead.SetString(buf);
	        delete[] buf;
	        //f.Read (&buffRead ,sizeof(buffRead ));
	}
	catch(CException *e)
	{
		e->AssertValid ();
	}
	f.Close ();
}
many thanks for your response.i have done this work,But with the error I faced.please help me.

1>------ Build started: Project: teststring, Configuration: Debug Win32 ------
1>Compiling...
1>teststringDlg.cpp
1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(214) : error C2228: left of '.GetLength' must have class/struct/union
1> type is 'TCHAR *'
1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(216) : error C2039: 'GetBuffer' : is not a member of 'test'
1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(13) : see declaration of 'test'
1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(218) : error C2039: 'ReleaseBuffer' : is not a member of 'test'
1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(13) : see declaration of 'test'
1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(227) : warning C4267: 'argument' : conversion from 'size_t' to 'UINT', possible loss of data
1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(229) : error C2039: 'SetString' : is not a member of 'test'
1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststring dlg.cpp(13) : see declaration of 'test'
1>Build log was saved at "file://c:\Documents and Settings\zahra d\Desktop\Project\teststring\teststring\Debug\Buil dLog.htm"
1>teststring - 4 error(s), 1 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========