CCFileUtils::getFileData() adding random characters on the end of XML file.

I have here my XML fil, broken down to a few entries:

        0
        Item 1
        100
    

    
        1
        Item 2
        100

Then, using CCFileUtils, I tried to get the file’s data using getFileData.
<pre>
const char** fileName = CCFileUtils::sharedFileUtils > fullPathFromRelativePath;
unsigned long tmpSize = 0;
unsigned char * xmlData = CCFileUtils::sharedFileUtils
> getFileData;
CCLOG;
</pre>
The problem is,**CCLOG* prints the XML file but it randomly adds other characters. This is what appears on the console:

        0
        Item 1
        100
    

    
        1
        Item 2
        100
    

\377\211\377\216\377\214\377\206\377\213\377\225\377\223\377\247\377\245\377\241\377\245\377\257\377\257\377\254\377\252\377\241\377\246\377\261\377\272\377\267\377\273\377\315\377\314\377\330\377\326\377\311\377\305\377\335\377\332\377\350\377\355\377\351\377\355\377\373\377\376\377

What’s more interesting is that the added characters are totally random. Sometimes they are added, sometimes they are not. Is there anyway to fix this?
I’ve made sure the Line Breaks on the XML are LF and not CRLF and are in UTF-8 encoding. Am I missing something? I use COCOS2D-X 2.0.4.

Thanks in advance.

You’re using returned data as zero-terminated string, which it isn’t. Copy data to
another buffer and add terminating zero at the end, or make CCString out of it.

I’m not sure I understand it correctly. LibXML requires the string data to be passed as unsigned char.
How would I add a terminating zero to an unsigned char?

I managed to get it working somehow by using *std::string::assign().
I had to use
reinterpret_cast(expression)* to make it work correctly.

Thanks.

Hi,

I was also facing the same problem. I am new at these, can you please explain how did you solve it using std::string::assign()?

Regards.

For string functions to know where to stop, strings must have a zero
at the end.

getFileData() is there to work with all kinds of data, not just
strings. It will ignore zeros and instead will just tell you how
much data there was in a file. What it returns is raw data, not a
string.

CCLOG () still expects a string and will need a zero to stop it. If
there isn’t one it will print the string, then random stuff from
memory after it all the way until it encounter zero somewhere.

CCString has createWithData() function - it will take data and
length and use them to construct CCString. OR you can allocate
another array large enough to hold string data plus one character,
copy string data there and cap it with zero.

A W wrote:

For string functions to know where to stop, strings must have a zero
at the end.
>
getFileData() is there to work with all kinds of data, not just
strings. It will ignore zeros and instead will just tell you how
much data there was in a file. What it returns is raw data, not a
string.
>
CCLOG () still expects a string and will need a zero to stop it. If
there isn’t one it will print the string, then random stuff from
memory after it all the way until it encounter zero somewhere.

CCString has createWithData() function - it will take data and
length and use them to construct CCString. OR you can allocate
another array large enough to hold string data plus one character,
copy string data there and cap it with zero.

Can you give me a little example on how to construct it with CCString?
I try this:

unsigned long tmpSize = 0;
unsigned char *fileContent = cocos2d::CCFileUtils::sharedFileUtils()->getFileData( "test.txt", "r", &tmpSize );

cocos2d::CCString ccStr( (char*) fileContent );
cocos2d::CCLog( "str: %s", ccStr.getCString() );

so the ccStr also has a bunch of a rubbish.
If I try to execute following code:

ccStr.createWithData( fileContent, tmpSize );

I get an empty ccStr. But tmpSize is 19;

So where cocos2d gets all those rubbish? My text file doesn’t have any rubbish in it…

A W wrote:

getFileData() is there to work with all kinds of data, not just
strings. It will ignore zeros and instead will just tell you how
much data there was in a file. What it returns is raw data, not a
string.

I understand that it’s not a string. But at the end of this “raw data” I see strange “.txt” - which doesn’t contain in my text file at all. I think it’s a bug because getFileData() reads more than any file contains. And the length of read data (variable tmpSize after reading) more than the real length of this file.

createWithData() function is static, you don’t need an instance
of CCString to call it. Whole its point is that it takes data
and makes CCString for you:

CCString* newString = CCString::createWithData(fileContent, tmpSize);

Then, if the size you get is wrong, thats another completely
different thing. How do you know it is wrong? What is text file
encoding? Does it contain byte-order mark? Text file size often
does not match number of characters in it.

And raw data still doesn’t have terminating zero. Every time
you try to use or view it as a string it will not work properly
because, well, it’s not a string. You’ll just get past its end
and get random garbage from memory after it.

Oops, I think it was my fault. When I execute this:

cocos2d::CCString *ccStr = cocos2d::CCString::createWithData( fileContent, tmpSize );

it worked fine! Thank you very much! Now it works! :slight_smile: