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
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…