String to Binary Files.

This is a discussion on String to Binary Files. within the C++ Programming forums, part of the General Programming Boards category; Hey I am having an issue storing string in binary format than reading them back. here is the code. Code: ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    32

    String to Binary Files.

    Hey I am having an issue storing string in binary format than reading them back.

    here is the code.

    Code:
    #include <iostream>
    #include <conio.h>
    #include <vector>
    #include <string.h>
    #include <map>
    #include <fstream>
    using namespace std;
    
    
    typedef map<int,string> ZMAP;
    typedef std::pair<int, string> Vp;
    ZMAP daMap;
    ZMAP Map2;
    
    int main()
    {
    
    	cout<<"1(Write) 2(Read): ";
    	int c;
    	cin>>c;
    	if(c == 1)
    	{
    		fstream ff;
    		
    		daMap.insert(ZMAP::value_type(0,"bob"));
    		daMap.insert(ZMAP::value_type(1,"bob1"));
    		daMap.insert(ZMAP::value_type(2,"bob2"));
    
    		ff.open("Test.file", ios::out | ios::binary);
    
    		vector<Vp> v;
    		v.reserve(daMap.size());
    		v.insert(v.begin(),daMap.begin(),daMap.end());
    		vector<Vp>::size_type sz = v.size();
    		ff.write(reinterpret_cast<char *>(&sz),sizeof(sz));
    		ff.write(reinterpret_cast<char *>(&v[0]),v.size() * sizeof(v[0]));
    		ff.close();
    	}else{
    		fstream fs;
    		fs.open("Test.file", ios::in | ios::binary);
    
    		vector<Vp>::size_type szv;
    		fs.read(reinterpret_cast<char *>(&szv),sizeof(szv));
    		vector<Vp> z(szv);
    		fs.read(reinterpret_cast<char *>(&z[0]),z.size() * sizeof z[0]);
    		Map2.insert(z.begin(),z.end());
    		for(ZMAP::iterator fsz = Map2.begin(); fsz!= Map2.end();++fsz)
    		{
    			cout<<(*fsz).first<<endl<<(*fsz).second<<endl;
    		}
    	}
    	cout<<"Done"<<endl;
    	getch();
    }
    The Output that i get when reading the data back in is...

    Code:
    0
    e(2
    1
      (2
    2
      (2
    Done
    Any advise on how to fix this is greatly appricated.

    The goal here is to take the Map data put it into a vector which is written into binary, since this is what was suggested by a few sites.

    Thanks again for any advice.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >since this is what was suggested by a few sites
    Those sites are probably wrong. Treating a non-POD type as a sequence of bytes is undefined, so your output could correctly be anything at all. Rather than try to use a non-portable and error-prone solution, try serializing your types to a string format, then write the result of c_str() to file.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21