473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to write a map to file.

hi all,

i want to write a map to a file and then read it again.
but while reading it is not wrorking.

the map declaration is:
Expand|Select|Wrap|Line Numbers
  1. typedef map<int,LogSettings> MapType;
  2. typedef MapType::value_type ValuePair;
  3.  
  4. where LogSettings is a structure:
  5.  
  6. struct LogSettings
  7.       {
  8.             unsigned long dwSession; 
  9.             TCHAR *szFilePath; 
  10.             TCHAR *szInfection; 
  11.             TCHAR *szAction;
  12.             TCHAR *szMessage; 
  13.             TINFECTIONSTATE iAction;
  14.       };
where TINFECTIONSTATE is just a enum.

to write to a file th function is:
Expand|Select|Wrap|Line Numbers
  1. int DataToFile((LogSettings &lgsettings)
  2. {
  3.     MapType Map;
  4.     Map.insert(ValuePair(1,lgsettings));
  5.     FILE* f = fopen("C:\\sample.txt","wb");
  6.                 size_t n = fwrite(&Map,sizeof(Map),1,f);
  7.     fclose(f);
  8.                 return (int)n;
  9. }
while writing the sturcture is initialised to some values.

To read the map:
Expand|Select|Wrap|Line Numbers
  1. MapType Map1;
  2.     FILE* fd = fopen("C:\\sample.txt","rb");
  3.     size_t nm = fread(&Map1,sizeof(Map1),1,fd);
  4.     fclose(fd);
  5.     MapType::iterator iter = Map1.begin();
  6.     //while(iter !=Map1.end())
  7.     {
  8.         cout<<(*iter).first<<endl;
  9.         cout<<(*iter).second.dwSession<<endl;
  10.         cout<<(*iter).second.iAction<<endl;
  11.         cout<<(*iter).second.szAction<<endl;
  12.         cout<<(*iter).second.szFilePath<<endl;
  13.         cout<<(*iter).second.szInfection<<endl;
  14.         cout<<(*iter).second.szMessage<<endl;
  15.     }

if i try to write the sturcture to file and when i read it again it is working properly.but when i take that to map what is the problem??
May 24 '07 #1
1 13828
weaknessforcats
9,208 Expert Mod 8TB
A map is a binary tree and you cannot write the map directly to disc becuse of the tree-ness and object-ness of the thing.

Instead, unload the map elements and write them do disc individually with separators so you can read them back.

To restore the map, read the disc file and insert the element into an empty map.

By writing to disc using the address of the map ASSUMES the elements are all following that address, like an array. This is not true. The map elements are objects and their data instances could be anywhere in memory.
May 24 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: clintonG | last post by:
The application is a simple form of three textbox controls that results in writing an XML file to the file system of the application root when it runs on the local development machine. I've just...
3
by: Lauren Quantrell | last post by:
I'd like to use my Access 2K application to write some text in the file properties of a Windows file. The properties I'm talking about are the properties you see whn you right click on a Windows...
16
by: ben beroukhim | last post by:
I have huge number of legacy code which use standard files functions. I would like to pass a memory pointer rather than a FILE pointer. I am trying to use FILEs in the code to refer to memory...
1
by: Phil396 | last post by:
I have to create a simple program but I want it to look as professional as possible. The program needs to open a file on a network share write the contents of a number of files to one file. The...
0
by: shefali | last post by:
Hi, I need to use an HTTPHandler to create and write files. I wanted to create the file in the directory that has this handler, and other handlers. For me, this directory is:...
1
by: Tim | last post by:
Hi, What I am trying to accomplish is to initiate remote desktop session from within my C# application to XP Pro machine NOT terminal services to a server. I have not found any way to do this...
1
by: Gregory Gadow | last post by:
I have a folder on \\webserver\wwwroot, called staticContent. The folder has several message of the day text files, such as RepMotd.text. I have an admin page, AdminMotd.aspx, that allows a user to...
5
by: sheriff | last post by:
Dear friends, im a newbee for this forum and c++ im doing my MSc in Simulation Tech in mech. Engineering. My knowledge of c++ is very little which I had during my UG studies Long long ago .I am...
0
by: Swan | last post by:
Can anyone plz tell me,Can I write file on server using binary access,or it is only to write on local?(Actually I am creating OCX for Http File Upload Control in VB using API's.So for that I need to...
4
by: Bruno | last post by:
Hi! I have big .txt file which i want to read, process and write to another .txt file. I have done script for that, but im having problem with croatian characters (Š,Đ,Ž,Č,Ć). How can I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.