功能:该函数将字符串写入文本文件.
函数形式: void cvWriteString( CvFileStorage* fs, const char*key, const char* value, int quote=0 )
参数列表:
fs: ,被写入的文本文件
key: 写入的文本字符串的名字
value:写入的文本字符串
quote: 当quote不为0时,无论是否需要,字符串被写入quote中。
当quote为0时,只有在需要的时候字符串被写入quote。
程序应用示例:
#include <cv.h>
#include <highgui.h>
int main(void)
{
// 创建文件存储对象
CvFileStorage *fs = cvOpenFileStorage("test.xml", 0, CV_STORAGE_WRITE);
// 写注释
cvWriteComment(fs, "测试写XML文件", 1);
cvWriteString(fs, "name", "刘越");
{
// 开始
cvStartWriteStruct(fs, "Name", CV_NODE_MAP);
}
// 结束
cvEndWriteStruct(fs);
// 释放文件存储对象
cvReleaseFileStorage(&fs);
// 文件节点
CvFileNode * node;
const char * str,*str1;
CvFileStorage *fs1 = cvOpenFileStorage("test.xml", 0, CV_STORAGE_READ);
// 获得第一层数据节点
node = cvGetFileNodeByName(fs1, 0, "node");
str = cvReadStringByName(fs1, node, "name");
str1 = cvReadString(node, "18");
printf("\n姓名=%s", str);
printf("\n年龄=%s", str1);
system("pause");
}
程序输出结果:
<?xml version="1.0"?>
<opencv_storage>
<!-- 测试写XML文件 -->
<name>"刘越"</name>
<Name>
</Name>
</opencv_storage>