# cv::FileStorage Class Reference

`#include <core.hpp>`

List of all members.

## Public Types

enum | { READ = 0, WRITE = 1, APPEND = 2 }
| file storage mode More...
enum | { UNDEFINED = 0, VALUE_EXPECTED = 1, NAME_EXPECTED = 2, INSIDE_MAP = 4 }

## Public Member Functions

CV_WRAP | FileStorage ()
| the default constructor
CV_WRAP | FileStorage (const string &filename, int flags)
| the full constructor that opens file storage for reading or writing
| FileStorage (CvFileStorage *fs)
| the constructor that takes pointer to the C FileStorage structure
CV_WRAP FileNode | getFirstTopLevelNode () const
| returns the first element of the top-level mapping
virtual CV_WRAP bool | isOpened () const
| returns true if the object is associated with currently opened file.
virtual CV_WRAP bool | open (const string &filename, int flags)
| opens file storage for reading or writing. The previous storage is closed with release()
const CvFileStorage * | operator* () const
| returns pointer to the underlying C FileStorage structure
CvFileStorage * | operator* ()
| returns pointer to the underlying C FileStorage structure
FileNode | operator[] (const string &nodename) const
| returns the specified element of the top-level mapping
CV_WRAP FileNode | operator[] (const char *nodename) const
| returns the specified element of the top-level mapping
virtual CV_WRAP void | release ()
| closes the file and releases all the memory buffers
CV_WRAP FileNode | root (int streamidx=0) const
| returns the top-level mapping. YAML supports multiple streams
void | writeObj (const string &name, const void *obj)
| writes the registered C structure (CvMat, CvMatND, CvSeq). See cvWrite()
void | writeRaw (const string &fmt, const uchar *vec, size_t len)
| writes one or more numbers of the specified format to the currently written structure
virtual | ~FileStorage ()
| the destructor. calls release()

## Static Public Member Functions

static string | getDefaultObjectName (const string &filename)
| returns the normalized object name for the specified file name

## Public Attributes

string | elname
| the currently written element
Ptr< CvFileStorage > | fs
| the underlying C FileStorage structure
int | state
| the writer state
vector< char > | structs
| the stack of written structures

## Detailed Description

XML/YAML File Storage Class.

The class describes an object associated with XML or YAML file. It can be used to store data to such a file or read and decode the data.

The storage is organized as a tree of nested sequences (or lists) and mappings. Sequence is a heterogenious array, which elements are accessed by indices or sequentially using an iterator. Mapping is analogue of std::map or C structure, which elements are accessed by names. The most top level structure is a mapping. Leaves of the file storage tree are integers, floating-point numbers and text strings.

For example, the following code:

     // open file storage for writing. Type of the file is determined from the extension
     FileStorage fs("test.yml", FileStorage::WRITE);
     fs << "test_int" << 5 << "test_real" << 3.1 << "test_string" << "ABCDEFGH";
     fs << "test_mat" << Mat::eye(3,3,CV_32F);

     fs << "test_list" << "[" << 0.0000000000001 << 2 << CV_PI << -3435345 << "2-502 2-029 3egegeg" <<
     "{:" << "month" << 12 << "day" << 31 << "year" << 1969 << "}" << "]";
     fs << "test_map" << "{" << "x" << 1 << "y" << 2 << "width" << 100 << "height" << 200 << "lbp" << "[:";

     const uchar arr[] = {0, 1, 1, 0, 1, 1, 0, 1};
     fs.writeRaw("u", arr, (int)(sizeof(arr)/sizeof(arr[0])));

     fs << "]" << "}";

will produce the following file:

     %YAML:1.0
     test_int: 5
     test_real: 3.1000000000000001e+00
     test_string: ABCDEFGH
     test_mat: !!opencv-matrix
         rows: 3
         cols: 3
         dt: f
         data: [ 1., 0., 0., 0., 1., 0., 0., 0., 1. ]
     test_list:
         - 1.0000000000000000e-13
         - 2
         - 3.1415926535897931e+00
         - -3435345
         - "2-502 2-029 3egegeg"
         - { month:12, day:31, year:1969 }
     test_map:
         x: 1
         y: 2
         width: 100
         height: 200
         lbp: [ 0, 1, 1, 0, 1, 1, 0, 1 ]

and to read the file above, the following code can be used:

     // open file storage for reading.
     // Type of the file is determined from the content, not the extension
     FileStorage fs("test.yml", FileStorage::READ);
     int test_int = (int)fs["test_int"];
     double test_real = (double)fs["test_real"];
     string test_string = (string)fs["test_string"];

     Mat M;
     fs["test_mat"] >> M;

     FileNode tl = fs["test_list"];
     CV_Assert(tl.type() == FileNode::SEQ && tl.size() == 6);
     double tl0 = (double)tl[0];
     int tl1 = (int)tl[1];
     double tl2 = (double)tl[2];
     int tl3 = (int)tl[3];
     string tl4 = (string)tl[4];
     CV_Assert(tl[5].type() == FileNode::MAP && tl[5].size() == 3);

     int month = (int)tl[5]["month"];
     int day = (int)tl[5]["day"];
     int year = (int)tl[5]["year"];

     FileNode tm = fs["test_map"];

     int x = (int)tm["x"];
     int y = (int)tm["y"];
     int width = (int)tm["width"];
     int height = (int)tm["height"];

     int lbp_val = 0;
     FileNodeIterator it = tm["lbp"].begin();

     for(int k = 0; k < 8; k++, ++it)
        lbp_val |= ((int)*it) << k;

## Member Enumeration Documentation

anonymous enum

file storage mode

Enumerator:

  READ |
  WRITE | read mode
  APPEND | write mode

anonymous enum

Enumerator:

  UNDEFINED |
  VALUE_EXPECTED |
  NAME_EXPECTED |
  INSIDE_MAP |

## Constructor & Destructor Documentation

CV_WRAP cv::FileStorage::FileStorage | ( | ) |

the default constructor

CV_WRAP cv::FileStorage::FileStorage | ( | const string & | filename,
|  | int | flags
| ) |  |

the full constructor that opens file storage for reading or writing

cv::FileStorage::FileStorage | ( | CvFileStorage * | fs ) |

the constructor that takes pointer to the C FileStorage structure

virtual cv::FileStorage::~FileStorage | ( | ) | `[virtual]`

the destructor. calls release()

## Member Function Documentation

static string cv::FileStorage::getDefaultObjectName | ( | const string & | filename ) | `[static]`

returns the normalized object name for the specified file name

FileNode cv::FileStorage::getFirstTopLevelNode | ( | ) | const`[inline]`

returns the first element of the top-level mapping

virtual CV_WRAP bool cv::FileStorage::isOpened | ( | ) | const`[virtual]`

returns true if the object is associated with currently opened file.

virtual CV_WRAP bool cv::FileStorage::open | ( | const string & | filename,
|  | int | flags
| ) |  | `[virtual]`

opens file storage for reading or writing. The previous storage is closed with release()

CvFileStorage* cv::FileStorage::operator* | ( | ) | `[inline]`

returns pointer to the underlying C FileStorage structure

const CvFileStorage* cv::FileStorage::operator* | ( | ) | const`[inline]`

returns pointer to the underlying C FileStorage structure

CV_WRAP FileNode cv::FileStorage::operator[] | ( | const char * | nodename ) | const

returns the specified element of the top-level mapping

FileNode cv::FileStorage::operator[] | ( | const string & | nodename ) | const

returns the specified element of the top-level mapping

virtual CV_WRAP void cv::FileStorage::release | ( | ) | `[virtual]`

closes the file and releases all the memory buffers

CV_WRAP FileNode cv::FileStorage::root | ( | int | streamidx = `0` ) | const

returns the top-level mapping. YAML supports multiple streams

void cv::FileStorage::writeObj | ( | const string & | name,
|  | const void * | obj
| ) |  |

writes the registered C structure (CvMat, CvMatND, CvSeq). See cvWrite()

void cv::FileStorage::writeRaw | ( | const string & | fmt,
|  | const uchar * | vec,
|  | size_t | len
| ) |  |

writes one or more numbers of the specified format to the currently written structure

## Member Data Documentation

string cv::FileStorage::elname

the currently written element

Ptr<CvFileStorage> cv::FileStorage::fs

the underlying C FileStorage structure

int cv::FileStorage::state

the writer state

vector<char> cv::FileStorage::structs

the stack of written structures

The documentation for this class was generated from the following files:

- /usr/obj/OpenCV-2.2.0/OpenCV-2.2.0/modules/core/include/opencv2/core/core.hpp
- /usr/obj/OpenCV-2.2.0/OpenCV-2.2.0/modules/core/include/opencv2/core/operations.hpp

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines