concatenating xmlChar* strings

1.2k Views Asked by At

I'm trying to contatenate xmlChar* strings to append ".rels" onto the filename. For some reason, I see the errors:

  1. error c2440 initializing cannot convert from const char[6] to const xmlChar *.
  2. A const_cast can only adjust type qualifiers; it cannot change the underlying type.

xmlChar is defined from xmlstring.h, libopc/libxml2, since I know not everyone knows about xmlChar.

xmlChar * temp = c->part_array[i].name; //this is a filename.doc with  path, has no compile error
                const xmlChar* temp2 = const_cast<xmlChar*>(".rels"); //"rels" here has error
                xmlStrcat(temp, temp2);

xmlStrcat wants xmlStrcat(xmlChar* cur, const xmlChar* add), which I think I have, once I get temp2 to be happy.

Any ideas? I'm having trouble finding xmlChar* examples where it's casted like this. I tried using just:

const xmlChar* temp2 = ".rels";

but get the error:

error c2440 initializing cannot convert from const char[6] to const xmlChar*

2

There are 2 best solutions below

0
On

I wound up having to use xmlStrndup and xmlCharStrndup

        xmlChar * temp = xmlStrndup(c->part_array[i].name, max_part_name);
        const char* tempA = ".rels";
        xmlChar* temp2 = xmlCharStrndup(tempA, sizeof(tempA));
        xmlStrcat(temp, temp2);
        const xmlChar* temp3 = (const xmlChar*)temp;
0
On

Direct casting worked for me :

enter image description here