Ask Your Question
0

File Input and Output using YAML files and static function

asked 2015-05-19 15:22:46 -0600

LBerger gravatar image

updated 2015-05-20 07:43:34 -0600

Hi, In this tutorial about yaml files function read and write are static.If you want to write your own data structures in an application with many source files static function is not very pratical. Is it really necessary to have static function? I try to write my own program which is made of three files B.h my class B, B.cpp read, write and operator << and main testYAML.cpp

and main

#include <opencv2/opencv.hpp> 
#include <iostream>
#include <map>
#include <fstream>
#include "B.h"

using namespace cv;
using namespace std;

int main (int argc,char **argv)
{
vector< Point> p;
B x,y;
x.entier["one"]=1;
x.entier["two"]=2;
x.reel["pi"]=acos(-1.0);
x.reel["e"]=exp(1);
{
FileStorage fs("test.yml",FileStorage::WRITE);
fs<<"operation"<<x;
x.write(fs);
}
FileStorage fs("test.yml",FileStorage::READ);

//y.read(fs);

// fs<<"Class B"<<x; I don't know how to do this
};

I cannot use something like this fs<<"CLASSB"<<x because="" i="" have="" got="" an="" error="" (vs2013="" opencv="" 3.0)<="" p="">

(error C2665: 'cv::write' : aucune des 9 surcharges n'a pu convertir tous les types d'arguments) error C2665: 'cv::write'  cannot convert argument

Thanks for your answer

B.h is

#ifndef __CLASS_B__
#define __CLASS_B__
#include <opencv2/opencv.hpp> 

#include <iostream>
#include <map>
#include <fstream>

class B {
    public :

    explicit B(){};
    std::map< cv::String,int> entier;
    std::map< cv::String,double> reel;

    void write(cv::FileStorage& fs) const;                        //Write serialization for this class
    void read(const cv::FileNode& node);                          //Read serialization for this class
};
    std::ostream& operator<<(std::ostream& out, const B& m);

#endif

B.cpp is

#include "B.h"

using namespace cv;
using namespace std;

void B::write(cv::FileStorage& fs)  const                       //Write serialization for this class
{
    int i=0;
    for ( auto  iti = entier.begin();iti!=entier.end();iti++,i++)
    {
        char tmp[256];
        sprintf(tmp,"%d",i);
        String s("keyInt");
        s += tmp;
        fs<<s<<iti->first;
        s= "ValueInt";
        s += tmp;
        fs<<s<<iti->second;
        }
    i=0;
    for ( auto  iti = reel.begin();iti!=reel.end();iti++,i++)
    {
        char tmp[256];
        sprintf(tmp,"%d",i);
        String s("keyReel");
        s += tmp;
        fs<<s<<iti->first;
        s= "ValueReel";
        s += tmp;
        fs<<s<<iti->second;
    }
}

void B::read(const cv::FileNode& node)                          //Read serialization for this class
{
    char tmp[256];
    int i=0,val;
    double x;
    String s,key;
    do
    {
        sprintf(tmp,"%d",i);
        s="keyInt";
        s += tmp;
        if(!node[s].empty())
        {
            key=(string)node[s];
            s= "ValueInt";
            s += tmp;
            if(!node[s].empty())
            {
                val=(int)node[s];
                entier[key]=val;
            }
        }
    }
    while (!node[s].empty());
    do
    {
        sprintf(tmp,"%d",i);
        s="keyReel";
        s += tmp;
        if(!node[s].empty())
        {
            key=(string)node[s];
            s= "ValueReel";
            s += tmp;
            if(!node[s].empty())
            {
                x=(double)node[s];
                entier[key]=val;
            }
        }
    }
    while (!node[s].empty());

}

static void write(FileStorage& fs, const std::string&, const B& x)
{
    x.write(fs);
}
static void read(const FileNode& node, B& x, const B& default_value = B()){
    if(node.empty())
        x = default_value;
    else
        x.read(node);
}

ostream& operator ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2015-05-20 11:21:39 -0600

LBerger gravatar image

Finally I read the manual. I don't how to use operator << and >> bu now I can save and read my own data using method write(fs) and read(fs).

#include <opencv2/opencv.hpp> 
#include <iostream>
#include <map>
#include <fstream>
#include "B.h"

using namespace cv;
using namespace std;

int main (int argc,char **argv)
{
vector< Point> p;
B x,y;
x.entier["one"]=1;
x.entier["two"]=2;
x.reel["pi"]=acos(-1.0);
x.reel["e"]=exp(1);
{
FileStorage fs("test.yml", FileStorage::WRITE);
x.write(fs);
}
FileStorage fs("test.yml",FileStorage::READ);

y.read(fs["Class_B"]);
cout<<"Class B Entier\n";
for (auto it = y.entier.begin(); it != y.entier.end(); it++)
    cout << it->first << "=" << it->second << "\n";
cout << "Class B reel\n";
for (auto it = y.reel.begin(); it != y.reel.end(); it++)
    cout << it->first << "=" << it->second << "\n";
// fs<<"Class B"<<x; I don't know how to do this
};

and

#include "B.h"

using namespace cv;
using namespace std;


void B::write(cv::FileStorage& fs)  const                       //Write serialization for this class
{
    int i=0;
    fs<<"mapint"<<"[";
    for (auto iti = entier.begin(); iti != entier.end(); iti++, i++)
    {
        fs<<"{:"<<"key"<<iti->first<<"val"<<iti->second<<"}";
    }
    i=0;
    fs << "]"<<"mapreel" << "[";
    for (auto iti = reel.begin(); iti != reel.end(); iti++)
    {
        fs << "{:" << "key" << iti->first << "val" << iti->second << "}";
    }
}

void B::read(const cv::FileNode& node)                          //Read serialization for this class
{
    FileNode mapint = node["mapint"];
    FileNodeIterator it = mapint.begin(), it_end = mapint.end();
    for (; it != it_end; ++it)
    {
        entier[(string)(*it)["key"]] = (int)(*it)["val"];
    }
    FileNode mapreel = node["mapreel"];
    FileNodeIterator itr = mapreel.begin(), itr_end = mapreel.end();
    for (; itr != itr_end; ++itr)
    {
        reel[(string)(*itr)["key"]] = (double)(*itr)["val"];
    }

}

and

#ifndef __CLASS_B__
#define __CLASS_B__
#include <opencv2/opencv.hpp> 

#include <iostream>
#include <map>
#include <fstream>
#include <stdio.h>

class B {
    public :

    explicit B(){};
    std::map< cv::String,int> entier;
    std::map< cv::String,double> reel;

    void write(cv::FileStorage& fs) const;                        //Write serialization for this class
    void read(const cv::FileNode& node);                          //Read serialization for this class
};

#endif
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-05-19 15:22:46 -0600

Seen: 356 times

Last updated: May 20 '15