FileStorage Read String Start With Number Need Quotation Mark 读取数字开头的字符串需要加引号

简介:
// Write data
    FileStorage fs("test.yml", FileStorage::WRITE);
    fs << "MyString" << "123abc";
    fs.release();
// Read data
    FileStorage fs2("test.yml", FileStorage::READ);
    string str = fs2["MyString"];
    cout << "MyString = " << str << endl;
    fs2.release();

In OpenCV, the FileStorage class can read and write data in the file. If you write the string "123abc" into the file, it will shows with quotation mark in the file. Now I want to read this string from the file into a string variable, you need to keep the quotation mark in the file which should be see as follows:

MyString: "123abc"

Now it can be read without problem, but if you change it to the followings:

MyString: 123abc

This will not gonna work this time, but if the string is not start with a number, it will work. I might treat it as a bug in the OpenCV. The three different situation is as follows:

MyString: "123abc"    // Work
MyString: 123abc      // NOT work
MyString: abc123      // Work

In sum, when you want to read a string into a variable, if it starts with a number, you need to add the quotation marker to the whole string in the file.

本文转自博客园Grandyang的博客,原文链接:读取数字开头的字符串需要加引号FileStorage Read String Start With Number Need Quotation Mark ,如需转载请自行联系原博主。

相关文章
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
691 100
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
791 99
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
695 92
|
安全 Java 测试技术
低调却重要:Java 字符串拼接,选 StringBuilder 还是 StringBuffer?
低调却重要:Java 字符串拼接,选 StringBuilder 还是 StringBuffer?
709 83
|
安全 Java API
【Java字符串操作秘籍】StringBuffer与StringBuilder的终极对决!
【8月更文挑战第25天】在Java中处理字符串时,经常需要修改字符串,但由于`String`对象的不可变性,频繁修改会导致内存浪费和性能下降。为此,Java提供了`StringBuffer`和`StringBuilder`两个类来操作可变字符串序列。`StringBuffer`是线程安全的,适用于多线程环境,但性能略低;`StringBuilder`非线程安全,但在单线程环境中性能更优。两者基本用法相似,通过`append`等方法构建和修改字符串。
478 1
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
689 14
|
数据处理
鸿蒙开发:ArkTs字符串string
字符串类型是开发中非常重要的一个数据类型,除了上述的方法概述之外,还有String对象,正则等其他的用处,我们放到以后得篇章中讲述。
1276 19