Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) boost::property_tree example 2: read and write and display

 

boost::property_tree example 2: read and write and display.

 

 

 

 

 

 

Technical facts

 

Application type(s)

Operating system(s) or programming environment(s)

IDE(s):

Project type:

C++ standard:

Compiler(s):

Libraries used:

 

 

 

 

 

Qt project file: CppProperty_treeExample2.pro

 

#-------------------------------------------------
#
# Project created by QtCreator 2011-08-13T07:58:58
#
#-------------------------------------------------
QT       += core
QT       -= gui
TARGET = CppProperty_treeExample2
CONFIG   += console
CONFIG   -= app_bundle
TEMPLATE = app
SOURCES += main.cpp

 

 

 

 

 

main.cpp

 

#include <string>
#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <cassert>
#include <fstream>
#include <iostream>
#include <string>

struct Person
{
  std::string m_name;
  int m_birth_year;

  void Load(const std::string &filename)
  {
    boost::property_tree::ptree t;
    boost::property_tree::xml_parser::read_xml(filename,t);
    m_name = t.get<std::string>("Person.name");
    m_birth_year = t.get<int>("Person.birth_year");

  }
  void Save(const std::string &filename) const
  {
    boost::property_tree::ptree t;
    t.put("Person.name", m_name);
    t.put("Person.birth_year", m_birth_year);
    boost::property_tree::xml_parser::write_xml(filename,t);
  }

};

std::ostream& operator<<(std::ostream& os,const Person& p)
{
  os << p.m_name
     << ' ' //Add either a space, tab or newline
     << p.m_birth_year;
  return os;
}

std::istream& operator>>(std::istream& is, Person& p)
{
  is
    >> p.m_name
    >> p.m_birth_year;
  return is;
}

int main()
{
  //Create a Person and save it to file
  {
    Person p;
    p.m_name = "Bilderbikkel";
    p.m_birth_year = 1980;
    std::ofstream f("tmp.txt");
    f << p;
  }
  //Load a Person from file and check it is the same
  {
    std::ifstream f("tmp.txt");
    Person p;
    f >> p;
    assert(p.m_name == "Bilderbikkel");
    assert(p.m_birth_year == 1980);
  }
  //Display tmp.txt
  {
    std::cout << "Displaying tmp.txt:\n";
    std::ifstream f("tmp.txt");
    while(!f.eof())
    {
      std::string s;
      std::getline(f,s);
      std::cout << s << '\n';
    }
  }

  //Create a Person and save it to XML file
  {
    Person p;
    p.m_name = "Bilderbikkel";
    p.m_birth_year = 1980;
    p.Save("tmp.xml");
  }
  //Load a Person from XML file and check it is the same
  {
    Person p;
    p.Load("tmp.xml");
    assert(p.m_name == "Bilderbikkel");
    assert(p.m_birth_year == 1980);
  }
  //Display tmp.xml
  {
    std::cout << "Displaying tmp.xml:\n";
    std::ifstream f("tmp.xml");
    while(!f.eof())
    {
      std::string s;
      std::getline(f,s);
      std::cout << s << '\n';
    }
  }
}

 

Screen output:

 

Displaying tmp.txt:
Bilderbikkel 1980
Displaying tmp.xml:
<?xml version="1.0" encoding="utf-8"?>
<Person><name>Bilderbikkel</name><birth_year>1980</birth_year></Person>

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict