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

Discussion in 'Win32' started by Gurjeet Singh Batth, Jan 4, 2008.

  1. Gurjeet Singh Batth

    Gurjeet Singh Batth New Member

    Joined:
    Nov 1, 2007
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Punjab
    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:
    /* [B]test[/B] is a structure defined in stdafx.h*/
    	/*
    	struct [B]test[/B]
    	{
    	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.
     
  2. zahra_d84

    zahra_d84 New Member

    Joined:
    Jan 13, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I was faced with this problem.Who can help me make.
     
  3. Gene Poole

    Gene Poole New Member

    Joined:
    Nov 10, 2009
    Messages:
    93
    Likes Received:
    5
    Trophy Points:
    0
    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;
    	[COLOR="SeaGreen"]size_t sz;
    	TCHAR *buf;[/COLOR]
    		
    	buffWrite .age ="27";
    	buffWrite .name = "My Name";
    
    	f.Open ("E:\\MyFile.bcr", CFile ::modeCreate | CFile ::modeWrite  ,&Fexcep);
    	[COLOR="SeaGreen"]sz=buffWrite.GetLength();
    	f.Write(&sz,sizeof(size_t));
    	f.Write(buffWrite.GetBuffer(),sz*sizeof(TCHAR));
    	//[/COLOR]f.Write (&buffWrite,sizeof(buffWrite ));
    	[COLOR="SeaGreen"]buffWrite.ReleaseBuffer();[/COLOR]
    	f.Close ();
    	
    	if(f.Open("E:\\MyFile.bcr", CFile ::modeRead ,&Fexcep) == FALSE)
    		return;
    	try
            {
    	        [COLOR="SeaGreen"]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;
    	        //[/COLOR]f.Read (&buffRead ,sizeof(buffRead ));
    	}
    	catch(CException *e)
    	{
    		e->AssertValid ();
    	}
    	f.Close ();
    }
    
     
    shabbir likes this.
  4. zahra_d84

    zahra_d84 New Member

    Joined:
    Jan 13, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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\teststringdlg.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\teststringdlg.cpp(216) : error C2039: 'GetBuffer' : is not a member of 'test'
    1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststringdlg.cpp(13) : see declaration of 'test'
    1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststringdlg.cpp(218) : error C2039: 'ReleaseBuffer' : is not a member of 'test'
    1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststringdlg.cpp(13) : see declaration of 'test'
    1>c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststringdlg.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\teststringdlg.cpp(229) : error C2039: 'SetString' : is not a member of 'test'
    1> c:\documents and settings\zahra d\desktop\project\teststring\teststring\teststringdlg.cpp(13) : see declaration of 'test'
    1>Build log was saved at "file://c:\Documents and Settings\zahra d\Desktop\Project\teststring\teststring\Debug\BuildLog.htm"
    1>teststring - 4 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice