From 7fc18fe146039928250db883e996c8cc98216b42 Mon Sep 17 00:00:00 2001 From: Leonid Beynenson Date: Thu, 28 Jun 2012 10:32:34 +0000 Subject: [PATCH] Added to the sample, demonstrating FileStorage, how to read/write data from/to std::string. --- samples/cpp/filestorage.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/samples/cpp/filestorage.cpp b/samples/cpp/filestorage.cpp index fa48fe63bf..402e21ffc2 100644 --- a/samples/cpp/filestorage.cpp +++ b/samples/cpp/filestorage.cpp @@ -150,7 +150,37 @@ int main(int ac, char** av) } - cout << "Try opening " << filename << " to see the serialized data." << endl; + cout << "Try opening " << filename << " to see the serialized data." << endl << endl; + + //read from string + { + cout << "Read data from string\n"; + string dataString = + "%YAML:1.0\n" + "mdata:\n" + " A: 97\n" + " X: 3.1415926535897931e+00\n" + " id: mydata1234\n"; + MyData m; + FileStorage fs(dataString, FileStorage::READ | FileStorage::MEMORY | FileStorage::FORMAT_YAML); + cout << "attempting to read mdata_b from string\n"; //Show default behavior for empty matrix + fs["mdata"] >> m; + cout << "read mdata\n"; + cout << m << endl; + } + + //write to string + { + cout << "Write data to string\n"; + FileStorage fs(filename, FileStorage::WRITE | FileStorage::MEMORY | FileStorage::FORMAT_YAML); + + cout << "writing MyData struct\n"; + MyData m(1); + fs << "mdata" << m; + cout << m << endl; + string createdString = fs.releaseAndGetString(); + cout << "Created string:\n" << createdString << "\n"; + } return 0; }