GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Sep-2009, 12:17
dpreznik dpreznik is offline
New Member
 
Join Date: Sep 2009
Posts: 7
dpreznik is on a distinguished road

Writing a map into file


Maybe somebody could please help me understand why I fail to write stl's map of vectors into a file using CFile::Write()?

CPP / C++ / C Code:
typedef std::vector<CLocation> LocationArray;
typedef std::map<int, LocationArray > MapLocationArray;


CPP / C++ / C Code:
		int iSize = sizeof(m_mapLocations);
		cf->Write(&iSize, sizeof(iSize));
		cf->Write(&m_mapLocations, iSize);


When I am writing, the m_mapLocations contains valid data. But when I read from file, it is empty, though the iSize is correct...

CPP / C++ / C Code:
		int iSize = 0;
		cf->Read(&iSize, sizeof(iSize));
		if(iSize > 0)
			cf->Read(&m_mapLocations, iSize);
Last edited by LuciWiz : 16-Sep-2009 at 12:31. Reason: Please insert your C++ code between [cpp] & [/cpp] tags
  #2  
Old 16-Sep-2009, 13:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,311
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Writing a map into file


Quote:
Originally Posted by dpreznik
Maybe somebody could please help me understand
The sizeof() operator tells something about the (implementation-dependent) amount of memory used to store information about an object. The value of a C++ sizeof() operation is constant and is determined at compile time.

It's probably the size of a couple of pointers, and the pointers point to "some place" in memory where the actual elements values are stored. You will have to access and store all of the elements individually to be able to save them in a file (they are not necessarily in contiguous memory as far as I can tell).

Regards,

Dave
  #3  
Old 17-Sep-2009, 05:09
dpreznik dpreznik is offline
New Member
 
Join Date: Sep 2009
Posts: 7
dpreznik is on a distinguished road

Re: Writing a map into file


Thank you Dave. This is what I suspected, too. I will store all elements separately, as you say.
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 07:44
After execution - Error cannot locate /Skin File? WSCH C++ Forum 1 05-Mar-2005 20:03
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 10:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 11:28
CD Buring Failed skanth2000 Computer Hardware Forum 1 15-Nov-2003 03:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:38.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.