(C, C++) char * compare, strcmp function
#include < stdio.h>
#include < string.h>
int main ()
{
char szKey[] = "apple";
if( strcmp (szKey,"apple") == 0)
printf("same\n");
else
printf("different\n");
return 0;
}
The study blog from marearts.com
Computer Vision & Machine Learning Research Laboratory
#include < stdio.h>
#include < string.h>
int main ()
{
char szKey[] = "apple";
if( strcmp (szKey,"apple") == 0)
printf("same\n");
else
printf("different\n");
return 0;
}
//string to char* string str = "hello"; char cstr[10]; strcpy(cstr,str.c_str()); // char * to string char cstr[] = "hello"; string str = cstr; //string(cstr)
< ? xml version="1.0" ?>
< LibraryIndex>
< !-- Settings for MyApp -->
< Messages>
< Welcome>Welcome to MyApp< /Welcome>
< Farewell>Thank you for using MyApp< /Farewell>
< /Messages>
< Windows>
< Window name="MainFrame" x="5" y="15" w="400" h="250" />
< /Windows>
< Connection ip="192.168.0.1" timeout="123.456000" />
< /LibraryIndex>
----#include < stdio.h>
#include "./tinyXML/tinyxml.h"
void main()
{
TiXmlDocument doc;
bool loadOkay = doc.LoadFile("ex4.xml");
if (loadOkay)
{
printf("open success\n");
}
else
{
printf("Failed to load file \n");
}
}
---#include < stdio.h>
#include "./tinyXML/tinyxml.h"
void main()
{
TiXmlDocument doc;
bool loadOkay = doc.LoadFile("ex4.xml");
if (loadOkay)
{
printf("open success\n");
}
else
{
printf("Failed to load file \n");
}
//root node access
TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex");
if (!pRoot) return;
//access "welcom" element to get value
TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");
if (!pElem) return;
//get element value (Welcome).
char* pszNode = (char*)pElem->Value();
if (pszNode)
printf("Node: %s\n", pszNode);
//get element text(Welcome to MyApp)
char* pszText = (char*)pElem->GetText();
if (pszText)
printf("Text: %s\n", pszText);
}
---#include < stdio.h>
#include "./tinyXML/tinyxml.h"
void main()
{
TiXmlDocument doc;
bool loadOkay = doc.LoadFile("ex4.xml");
if (loadOkay)
{
printf("open success\n");
}
else
{
printf("Failed to load file \n");
}
// access root node
TiXmlElement* pRoot = doc.FirstChildElement("LibraryIndex");
if (!pRoot) return;
// access welcome element to modify value
TiXmlElement* pElem = pRoot->FirstChildElement("Messages")->FirstChildElement("Welcome");
if (!pElem) return;
// element clear and new value adding
pElem->Clear();
pElem->LinkEndChild( new TiXmlText("Edit My App"));
doc.SaveFile("m_ex4.xml");
}
***#include < stdio.h>
#include "./tinyXML/tinyxml.h"
void main()
{
// Declaration XML format
TiXmlDocument doc;
TiXmlDeclaration* pDecl = new TiXmlDeclaration("1.0", "euc-kr", "");
doc.LinkEndChild(pDecl);
//Add Root node
TiXmlElement* pRoot = new TiXmlElement("LibraryIndex2");
doc.LinkEndChild(pRoot);
// Add Comment sentence
TiXmlComment * pComment = new TiXmlComment();
pComment->SetValue("Settings for MyApp");
pRoot->LinkEndChild(pComment);
// Add child node and data
TiXmlElement* pElem = new TiXmlElement("Messages");
pRoot->LinkEndChild(pElem);
TiXmlElement* pSubElem = new TiXmlElement("Welcome");
pSubElem->LinkEndChild( new TiXmlText("Welcome to MyApp"));
pElem->LinkEndChild(pSubElem);
pSubElem = new TiXmlElement("Farewell");
pSubElem->LinkEndChild( new TiXmlText("Thank you for using MyApp"));
pElem->LinkEndChild(pSubElem);
// Add child node and set attribute
pElem = new TiXmlElement("Windows");
pRoot->LinkEndChild(pElem);
pSubElem = new TiXmlElement("Window");
pElem->LinkEndChild(pSubElem);
pSubElem->SetAttribute("name", "MainFrame");
pSubElem->SetAttribute("x", 5);
pSubElem->SetAttribute("y", 15);
pSubElem->SetAttribute("w", 400);
pSubElem->SetAttribute("h", 250);
pElem = new TiXmlElement("Connection");
pRoot->LinkEndChild(pElem);
pElem->SetAttribute("ip", "192.168.0.1");
pElem->SetDoubleAttribute("timeout", 123.456);
//Save xml file format
doc.SaveFile("new_ex4.xml");
}
***#include < stdio.h>
#include "./tinyXML/tinyxml.h"
void searchXMLData(TiXmlElement* pElem)
{
TiXmlHandle hRoot(0);
TiXmlElement* pSubElem = pElem;
TiXmlAttribute* pAttrib = NULL;
//Set current node to root node and determine childe node is exist or not
hRoot = TiXmlHandle(pSubElem);
pSubElem = hRoot.FirstChildElement().Element();
if (!pSubElem) return;
char* pszNode = NULL;
char* pszAttrib = NULL;
char* pszText = NULL;
while (pSubElem)
{
//node
pszNode = (char*)pSubElem->Value();
if (pszNode)
printf("Node: %s\n", pszNode);
//Attribute
pAttrib = pSubElem->FirstAttribute();
while (pAttrib)
{
char* pszAttrib = (char*)pAttrib->Name();
char* pszText = (char*)pAttrib->Value();
printf("------------Attribute: %s, Data: %s\n", pszAttrib, pszText);
pAttrib = pAttrib->Next();
}
//Data
pszText = (char*)pSubElem->GetText();
if (pszText)
printf("Text: %s\n", pszText);
// Recursive call for searching child node based current node
searchXMLData(pSubElem);
pSubElem = pSubElem->NextSiblingElement();
}
}
int main()
{
TiXmlDocument doc;
doc.LoadFile("ex4.xml");
TiXmlHandle hDoc(&doc);
//access root node
TiXmlElement* pRoot = hDoc.FirstChildElement().Element();
if (!pRoot) return 0;
//search childe node step by step from starting root node
searchXMLData(pRoot);
return 0;
}