You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Qian Xia <xi...@kick.gr.jp> on 2000/12/01 12:12:10 UTC

using sprintf() to create a TextNode

Hi, I have a problem which I can't find the reason at all. So I come here
again for some help. Thanks very much.

I want to write int data in a xml file. So I choosed sprintf(). The main
part of the source is as the following: (under Solaris 2.6)

    unsigned int a = 200;
    char* chara = "\0";

    sprintf(chara,"%d",a);

// doc is the DOM_Document object created in the previous part of the src
    DOM_Element  prodElem = doc.createElement("Data");

    rootElem.appendChild(prodElem);

    DOM_Text    prodDataVal = doc.createTextNode(result);
    prodElem.appendChild(prodDataVal);

The result I got was:
0<?xml version="1.0" encoding="utf-8" ?><Test><Data>200</Data></Test>

The "0" appeared in the beginning! I even tried using DOMString.::transcode,
but the result was the same. Would there be anything wrong with sprintf()?
How to cope with int data in XML?

Thanks a lot,

_/_     Join those who intrigue you         _/_
_/_       and try to become intriguing too  _/_
 _/_ _______________________________________  _/_
  _/_    Name : Qian Xia                    _/_
   _/_   Email : xqianxia@ysb.nsd.co.jp _/_
    _/_ ____________________________________ _/_


Re: using sprintf() to create a TextNode

Posted by Qian Xia <xi...@kick.gr.jp>.
Thank you sir.
I tried what you said, just define chara the other way:

char chara[100];

And then, no problem any more!

Thanks again,
_/_     Join those who intrigue you         _/_
_/_       and try to become intriguing too  _/_
 _/_ _______________________________________  _/_
  _/_    Name : Qian Xia                    _/_
   _/_   Email : xqianxia@ysb.nsd.co.jp _/_
    _/_ ____________________________________ _/_



| On Fri, Dec 01, 2000 at 08:12:10PM +0900, Qian Xia wrote:
| > Hi, I have a problem which I can't find the reason at all. So I come
here
| > again for some help. Thanks very much.
| >
| > I want to write int data in a xml file. So I choosed sprintf(). The main
| > part of the source is as the following: (under Solaris 2.6)
| >
| >     unsigned int a = 200;
| >     char* chara = "\0";
| >
| >     sprintf(chara,"%d",a);
| chara points to static memory of size 2 (including extra '\0') so you can
not
| sprintf 4 bytes (sizeof("200")) to it
|
| try make data section readonly
| the Sun CC accepts the option -xMerge which merges data and text section.
| since text section is read-only all initialized static static data gets
| read-only also with -xMerge you get segmentation fault
|
|
| try the following
|
| unsigned int a = 200;
| char data[100];
| snprintf(data, sizeof(data), "%d", a);
|
| archaic Sun systems does not implement snprintf but __snprintf without
| signature (mess) in header file so it is a good stuff for configure script
|
| --
| I'd be pleased with any feedback. In the absence of such I'll make my own
| choices. Actually, though, I'd love and answer any question.
| ===================================
| Miroslaw Dobrzanski-Neumann
|
| MOSAIC SOFTWARE AG
| Abteilung Basisentwicklung und Forschung
| Tel +49-2225-882-291
| Fax +49-2225-882-201
| E-mail: mne@mosaic-ag.com
|
|
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
| For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
|
|


Re: using sprintf() to create a TextNode

Posted by Miroslaw Dobrzanski-Neumann <mn...@mosaic-ag.com>.
On Fri, Dec 01, 2000 at 08:12:10PM +0900, Qian Xia wrote:
> Hi, I have a problem which I can't find the reason at all. So I come here
> again for some help. Thanks very much.
> 
> I want to write int data in a xml file. So I choosed sprintf(). The main
> part of the source is as the following: (under Solaris 2.6)
> 
>     unsigned int a = 200;
>     char* chara = "\0";
> 
>     sprintf(chara,"%d",a);
chara points to static memory of size 2 (including extra '\0') so you can not
sprintf 4 bytes (sizeof("200")) to it 

try make data section readonly
the Sun CC accepts the option -xMerge which merges data and text section.
since text section is read-only all initialized static static data gets
read-only also with -xMerge you get segmentation fault


try the following

unsigned int a = 200;
char data[100];
snprintf(data, sizeof(data), "%d", a);

archaic Sun systems does not implement snprintf but __snprintf without
signature (mess) in header file so it is a good stuff for configure script

-- 
I'd be pleased with any feedback. In the absence of such I'll make my own
choices. Actually, though, I'd love and answer any question.
===================================
Miroslaw Dobrzanski-Neumann

MOSAIC SOFTWARE AG
Abteilung Basisentwicklung und Forschung
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: mne@mosaic-ag.com