Boost logo

Boost Users :

From: Stephane Martineau (stephane.martineau_at_[hidden])
Date: 2005-08-02 03:55:25


Hye,

this little program fail in serialization of a float with binary archive but success with xml or text archive ??

as i am not an expert, i have now idea why ???
 i have the save problem with double and like with float, only depending of the value ....

it fails under windows XP, VC++7.1 but success under Linux + gcc

thanks for helping ,
Stéphane.

// problemeserialisation.cpp
//

#include <iostream>
#include <fstream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>

void test_binary (float & f)
{
 using namespace std ;
 std::ofstream ofs("val.bin");
 boost::archive::binary_oarchive oa(ofs,std::ios::binary);
 oa<< f ;
 ofs.close();
 std::cout << "Valeur serialisee binary " << f << std::endl ;
 std::ifstream ifs("val.bin");
 boost::archive::binary_iarchive ia(ifs,std::ios::binary);
 float f1;
 ia >> f1;
 
if (ifs.fail())
 {
  cout << "badbit " << ( ifs.rdstate( ) & ios::badbit ) << endl;
  cout << "failbit " << ( ifs.rdstate( ) & ios::failbit ) << endl;
  cout << "eofbit " << ( ifs.rdstate( ) & ios::eofbit ) << endl;
 }

 std::cout << "Valeur deserialisee binary "<< f1 << std::endl ;
    ifs.close();
 return ;
}

void test_text (float & f)
{
 std::ofstream ofs("val.txt");
    boost::archive::text_oarchive oa(ofs);
 oa << f ;
 ofs.close();
 std::cout << "Valeur serialisee Texte " << f << std::endl ;
 std::ifstream ifs("val.txt");
 boost::archive::text_iarchive ia(ifs);
 float f2 ;
 ia >> f2;
 std::cout << "Valeur deserialisee Texte "<< f2 << std::endl ;
    ifs.close();
 return ;
}

void test_xml (float & f)
{
 std::ofstream ofs("val.xml");
    boost::archive::xml_oarchive oa(ofs);
 oa << BOOST_SERIALIZATION_NVP(f) ;
 ofs.close();
 std::cout << "Valeur serialisee Xml " << f << std::endl ;
 std::ifstream ifs("val.xml");
 boost::archive::xml_iarchive ia(ifs);
 float f2 ;
 ia >> BOOST_SERIALIZATION_NVP(f2);
 std::cout << "Valeur deserialisee Xml "<< f2 << std::endl ;
    ifs.close();
 return ;
}

int main (int argc, const char * argv[])
{
 float f1 = (float)2.411667 ;
 test_text(f1);
 test_xml(f1);
 test_binary(f1);
 return 0;
}

output :

Valeur serialisee Texte 2.41167
Valeur deserialisee Texte 2.41167
Valeur serialisee Xml 2.41167
Valeur deserialisee Xml 2.41167
Valeur serialisee binary 2.41167
badbit 0
failbit 2
eofbit 1
Valeur deserialisee binary -1.07137e+008



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net