0

Am working in vc++ and trying to load an xml file and load the entire data into a string but am not getting the results

 char text[700] = {""};

 TiXmlDocument doc( "'demotest.xml" );
 bool loadOkay = doc.LoadFile();
 if ( !loadOkay )
 {
    printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
    system("PAUSE");
    exit( 1 );
}

    printf( "** Demo doc read from disk: ** \n\n" );
    printf( "** Printing via doc.Print **\n" );
    //doc.Print( stdout );

    {
        printf( "** Printing via TiXmlPrinter **\n" );
        TiXmlPrinter printer;
        doc.Accept( &printer );
        fprintf( stdout, "%s", printer.CStr() );

//upto this line its working fine in console. but when I convert this string am getting struck

        wsprintf(text, "%s", (char*)printer.CStr());
        AddLOG_message(text, 0, true);



    }

Last two lines I should get the entire content of the xml including header, elements and values. Please help.

acraig5075
  • 10,588
  • 3
  • 31
  • 50
user1441251
  • 81
  • 3
  • 8

1 Answers1

0

I'd do it like this, with less C code, more C++ code and deprecating the risky char array of length magic number 700:

TiXmlPrinter printer;
doc.Accept( &printer );
doc.Print(); // simpler for stdout output
std::string text = printer.CStr(); // easier, safer this way
AddLOG_message( text.c_str(), 0, true );
acraig5075
  • 10,588
  • 3
  • 31
  • 50
  • Thank you very much. my addlog_message function is ------- >>>>> void AddLOG_message(char *message, int len, int add_time) if I print -- >> char texts[700] = {"136"}am getting only 136 in return. I need the full xml structure in the string. – user1441251 Jan 11 '13 at 11:20
  • @user1441251 Can you make the parameter `const char *message`? Otherwise my answer won't work, and you'd still need `char text[700]`, but then easier to `strncpy(text, printer.CStr(), 699);` – acraig5075 Jan 11 '13 at 11:27
  • I have mapped a lot with this char*. If I change to const char* then am getting error "const char is incompatible with parameter of type TCHAR " void AddLOG_message(const char *message, int len, int add_time) Please help me in this – user1441251 Jan 11 '13 at 11:58