一、c++读写xml文件
1、定义了两个类,
将需要的数据,按字符串写进指针buf,然后保存xml文件,再读出来,写进别的类任务,将字符串转换为数字,进行计算
#define CONFIG_FILE_CALIB "/root/config/config.xml"
using namespace cv;
using namespace std;
double fishcorrect_value[14]={0,};
class Top//定义了一个大类
{
public:
explicit Top();
~Top();
static Top *getInstance();
CThread *config; //其中一个类
CThread *rectify; //其中一个类
static class Top *instance;
};
class CRectify:public //定义了一个类
{
public:
CRectify(void *is, SX_S8 *name, SX_S32 id);
~CRectify();
virtual SX_S32 open(void *arg);
private:
Top *is;
};
class Top;
class CConfig:public //定义了一个类
{
public:
explicit CConfig(void *is, SX_S8 *name, SX_S32 id);
~CConfig();
virtual SX_S32 open(void *arg);
char light_centrerow1[100];
char light_centrecol1[100];
char light_K4[100];
char light_K3[100];
char light_K2[100];
char light_K1[100];
char light_K0[100];
private:
Top *is; SX_S32 saveFile(SX_S8 *data, SX_S32 len, SX_S8 *filename);
SX_S32 readFile( SX_S8 *filename, SX_S8 *buf, SX_S32 buflen);
};
SX_S32 CConfig::saveFile( SX_S8 *data, SX_S32 len, SX_S8 *filename)
{
if( data == NULL || filename == NULL)
{
TRACE(DL_ERROR, "DL_ERROR\n");
return -1;
}
if( (len < 0) || (len > (MAX_CONFIG_FILE_LEN) ))
{
TRACE(DL_ERROR, "DL_ERROR\n");
return -1;
}
/* //TODO
data check, CRC check
*/
FILE *fd = fopen( filename, "wb" );
if( !fd )
{
TRACE(DL_ERROR, "DL_ERROR\n");
return -1;
}
if ( fwrite( data, 1, len, fd ) != len )
{
TRACE(DL_ERROR, "DL_ERROR\n");
fclose(fd);
return -1;
}
fclose( fd );
return len;
}
SX_S32 CConfig::readFile( SX_S8 *filename, SX_S8 *buf, SX_S32 buflen)
{
SX_S32 s32Ret = 0;
SX_S32 s32ReadLen = 0;
if( buf == NULL || filename == NULL)
{
TRACE(DL_ERROR, "DL_ERROR\n");
return -1;
}
memset( buf, 0, buflen );
FILE *fd = fopen( filename, "rb" );
if( !fd )
{
TRACE(DL_ERROR, "DL_ERROR\n");
return -1;
}
fseek(fd, 0, SEEK_END);
s32Ret = ftell(fd);
if( s32Ret > buflen )
{
s32ReadLen = buflen;
TRACE(DL_WARNING, "DL_WARNING\n");
}
else
{
s32ReadLen = s32Ret;
}
fseek(fd, 0, SEEK_SET);
s32Ret = fread( buf, 1, s32ReadLen, fd);
fclose( fd );
return s32Ret;
}
SX_S32 xmlGetInt(const SX_S8 *szXml, const SX_S8 *szTitle, SX_S32 defaultValue)
{
char buf[255] = { 0 };
const char *ptr1 = NULL;
const char *ptr2 = NULL;
int ilen1, ilen2;
sprintf_s(buf, sizeof(buf), "<%s>", szTitle);
ilen1 = strlen(buf);
ptr1 = strstr(szXml, buf);
sprintf_s(buf, sizeof(buf), "</%s>", szTitle);
ilen2 = strlen(buf);
ptr2 = strstr(szXml, buf);
if (ptr1 && ptr2)
{
char szval[255] = { 0 };
memcpy(szval, ptr1 + ilen1, ptr2 - ptr1 - ilen1);
return atoi(szval);
}
else
return defaultValue;
}
SX_S32 xmlGetStr(const SX_S8 *szXml, const SX_S8 *szTitle, SX_S8 *szValue)
{
char buf[255] = { 0 };
const char *ptr1 = NULL;
const char *ptr2 = NULL;
int ilen1, ilen2;
sprintf_s(buf, sizeof(buf), "<%s>", szTitle);
ilen1 = strlen(buf);
ptr1 = strstr(szXml, buf);
sprintf_s(buf, sizeof(buf), "</%s>", szTitle);
ilen2 = strlen(buf);
ptr2 = strstr(szXml, buf);
if (ptr1 && ptr2)
{
int ibytes = ptr2 - ptr1 - ilen1;
memcpy(szValue, ptr1 + ilen1, ibytes);
szValue[ibytes] = 0;
return strlen(szValue);
}
else
{
//*szValue = 0;
return 0;
}
}
std::vector<std::string> split(const std::string& s, char delimiter)
{
std::vector<std::string> tokens;
std::string token;
std::istringstream tokenStream(s);
while (std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
CRectify::CRectify(void *is, SX_S8 *name, SX_S32 id):
CThread(is, name, id)
{
this->is = (Top *)is;
}
CRectify::~CRectify()
{
this->is = NULL;
}
SX_S32 CConfig::open(void *arg)
{
sprintf_s(light_centrerow1, sizeof(light_centrerow1), "0");
sprintf_s(light_centrecol1, sizeof(light_centrecol1), "0");
sprintf_s(light_K4, sizeof(light_K4), "0");
sprintf_s(light_K3, sizeof(light_K3), "0");
sprintf_s(light_K2, sizeof(light_K2), "0");
sprintf_s(light_K1, sizeof(light_K1), "0");
sprintf_s(light_K0, sizeof(light_K0), "0");
if( saveFile( ptUdp->pszConfigData, ptUdp->s32ConfigLength, CONFIG_FILE_CALIB ) > 0 ) **//pszConfigData 是按字符写入的数据**
{
s32ConfigLength = readFile( CONFIG_FILE_CALIB, pszConfigData, MAX_CONFIG_FILE_LEN);
if( s32ConfigLength > 0 )
{
xmlGetStr((const SX_S8 *)pszConfigData, "szCenterRow1", light_centrerow1);
xmlGetStr((const SX_S8 *)pszConfigData, "szCenterCol1", light_centrecol1);
xmlGetStr((const SX_S8 *)pszConfigData, "szCoef11", light_K4);
xmlGetStr((const SX_S8 *)pszConfigData, "szCoef12", light_K3);
xmlGetStr((const SX_S8 *)pszConfigData, "szCoef13", light_K2);
xmlGetStr((const SX_S8 *)pszConfigData, "szCoef14", light_K1);
xmlGetStr((const SX_S8 *)pszConfigData, "szCoef15", light_K0);
offsetX[0] = xmlGetInt((const SX_S8 *)pszConfigData, "posX", offsetX[0]);
}
}
}
SX_S32 CRectify::open(void *arg)
{
**// 将别的类的数据字符串,按,为标识区别取数据,**
std::vector<std::string> slist11 = split(std::string(((CConfig *)(is->config))->light_centrerow1), ',');
if (slist11.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist12 = split(std::string(((CConfig *)(is->config))->light_centrecol1), ',');
if (slist12.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist13 = split(std::string(((CConfig *)(is->config))->light_K4), ',');
if (slist13.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist14 = split(std::string(((CConfig *)(is->config))->light_K3), ',');
if (slist14.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist15 = split(std::string(((CConfig *)(is->config))->light_K2), ',');
if (slist15.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist16 = split(std::string(((CConfig *)(is->config))->light_K1), ',');
if (slist16.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist17 = split(std::string(((CConfig *)(is->config))->light_K0), ',');
if (slist17.size() != 1){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist5 = split(std::string(((CConfig *)(is->config))->R), ',');
if (slist5.size() != 9){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
std::vector<std::string> slist6 = split(std::string(((CConfig *)(is->config))->T), ',');
if (slist6.size() != 3){
TRACE(DL_ERROR, "calibrate mat error\n");
close();
return -1;
}
//这里是opcv矩阵,给矩阵读取值,
cv::Mat R = cv::Mat::zeros(3, 3, CV_64F);//建立一个3x3的矩阵
// 1 2 3
// 4 5 6
// 7 8 9
cv::Mat T = cv::Mat::zeros(3, 1, CV_64F);//建立一个3x1的矩阵
// 1 2 3
for (int row = 0; row < 3; row++){
for (int col = 0; col < 3; col++){
double value;
value = atof(slist5.at(row * 3 + col).c_str());
R.at<double>(col,row) = value;
}
}
for (int row = 0; row < 3; row++) {
T.at<double>(row, 0) = atof(slist6.at(row).c_str());
}
//
fishcorrect_value[0] = atof(slist11.at(0).c_str()); //将矩阵里的字符转为浮点型输出
fishcorrect_value[1] = atof(slist12.at(0).c_str());
fishcorrect_value[2] = atof(slist13.at(0).c_str());
fishcorrect_value[3] = atof(slist14.at(0).c_str());
fishcorrect_value[4] = atof(slist15.at(0).c_str());
fishcorrect_value[5] = atof(slist16.at(0).c_str());
fishcorrect_value[6] = atof(slist17.at(0).c_str());
cout << "\nT:\n" << T;
cout << "\nR:\n" << R;
cout << "\r\n ++++++++++++++++++++++++ \r\n";
cout << "\n @@@@@@@ light_centrerow1:\n" << fishcorrect_value[0];
cout << "\n @@@@@@@ light_centrecol1:\n" << fishcorrect_value[1];
cout << "\n @@@@@@@ light_K4:\n" << fishcorrect_value[2];
cout << "\n @@@@@@@ light_K3:\n" << fishcorrect_value[3];
cout << "\n @@@@@@@ light_K2:\n" << fishcorrect_value[4];
cout << "\n @@@@@@@ light_K1:\n" << fishcorrect_value[5];
cout << "\n @@@@@@@ light_K0:\n" << fishcorrect_value[6];
cout << "\r\n ++++++++++++++++++++++++ \r\n";
}
二、最后//config_calib.xml的内容
<R>-0.329870805561080,0.646655876290352,0.687765533665101,-0.329163289905405,-0.761610901625588,0.558211754716532,0.884780639603475,-0.0422494045427442,0.464088577319727</R>
<T>-2497.39116026903,1459.45403169311,-905.633728173451</T>
<szCenterRow1>307.5</szCenterRow1>
<szCenterCol1>377.5</szCenterCol1>
<szCoef11>-8.0757e-06</szCoef11>
<szCoef12>0.0013191</szCoef12>
<szCoef13>-0.066433</szCoef13>
<szCoef14>8.2024</szCoef14>
<szCoef15>5.716</szCoef15>