TripleS
TripleS

Reputation: 1246

Is that possible to use a string as a name in opencv FileStorage

I'd like to write an XML file using opencv FileStorage object.

Most of the examples I get to see works like the following

FileStorage fs("d:\\1.xml" , FileStorage::WRITE); 
fs << "one" << 1; 

I'd like to write a section with modified name variable , which means that the text would not have to be inserted in compile time. I look for something like this:

 string st = "1"; 
 fs << st.c_str() << 1; 

However I keep on getting run time error. it's refused to work, I have tried using opencv string type, stl string type , char*, but still can't make it work.

Upvotes: 5

Views: 807

Answers (1)

Яois
Яois

Reputation: 3858

This is a tricky one. Looking in persistence.cpp I have found that the error is thrown because cv_isalpha is checked on the variable name. This means that your variable name should be beginning with a letter!

Try doing the following:

int main() {
    string oName; 
    oName = "a"; // Now, THIS will crash if you assign "number" characters, e.g "1"
    FileStorage fs("test.yml", FileStorage::WRITE);
    fs << oName.c_str() << 1 ;
    return 0;
}

This should work.

EDITED: you can put numbers in your 'string' variable as long as you begin with a letter (e.g. "a1"), it works too.

Upvotes: 4

Related Questions