BUG in the CCFileUtils::sharedFileUtils()->getFileData()? Rubbish in the read data.

My code:

unsigned char *fileContent = cocos2d::CCFileUtils::sharedFileUtils()->getFileData( "testfile.txt", "r", &tmpSize );
cocos2d::CCString *ccStr = cocos2d::CCString::createWithData( fileContent, tmpSize );

cocos2d::CCLog( "str: %s; size: %i", ccStr->getCString(), tmpSize );

std::string fileContentStr( ccStr->getCString() );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );

My file has codepage: UTF-8. Not everytime but every 5 / 10 attempts I see the output something like this:
———————
normal letters…20\243….right letters…20\236……
———————

What’s the “20\243”? 320… it’s not impossible for my language :slight_smile: so this is the error of reading data by getFileData().
“20\236” — there’s no such characters in whole UTF-8 charset! :))

Maybe the problem is only with CCLog… trying to find out.

try

unsigned char *fileContent = cocos2d::CCFileUtils::sharedFileUtils()->getFileData( “testfile.txt”, “rb”, &tmpSize );

chao li wrote:

try
unsigned char *fileContent = cocos2d::CCFileUtils::sharedFileUtils()>getFileData;
Tried but no success. The problem still stays. But you know… I’ve scanned whole string that returned to me from getFileData
there’s NO such char-codes as 320! I’ve checked with this code:

int charCode;
for ( unsigned int i = 0; i < str.length(); i++ ) {
    charCode = int( ( unsigned char ) str.at( i ) );
    if ( charCode > 230 ) {
        cocos2d::CCLog( "BAD CHAR!!!" );
    }
}

There’s no log rec “BAD CHAR!” in my log. Am I check it right? There’s no any chars with “id” more than 230. Maybe 20 - is in other format? I checked it wrong? So if I’m right - I think the problem in the CCLog.

e……Maybe it’s not a bug.
Function “getFileData” reads file as binary.
So your
@
fileContent = CCFileUtils::sharedFileUtils()>getFileData;
@
fileContent[tmpSize] maybe not equal to ‘’? right?
then? if your file is a text,add a line code as
@
fileContent[tmpSize] = ‘’;
@
or … other, Sorry by my poor English
-! Hope can help you!

Haojun Huang
Thanks for trying to help me! I’m very appreciate it!

e……Maybe it’s not a bug.
Function “getFileData” reads file as binary.
So your
@
fileContent = CCFileUtils::sharedFileUtils()->getFileData( “testfile.txt”, “rb”, &tmpSize );
@
fileContent[tmpSize] maybe not equal to ’

OMG… i think the bug in CCLog. I do following:

unsigned char *fileContent = cocos2d::CCFileUtils::sharedFileUtils()->getFileData( "testfile.txt", "r", &tmpSize );
cocos2d::CCString *ccStr = cocos2d::CCString::createWithData( fileContent, tmpSize );

cocos2d::CCLog( "str: %s; size: %i", ccStr->getCString(), tmpSize );

std::string fileContentStr( ccStr->getCString() );

cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );
cocos2d::CCLog( "fileContentStr: %s; size: %i", fileContentStr.c_str(), tmpSize );

so I see DIFFERENT outputs. It’s impossible! I don’t change the string here! “20\240” - in different parts of each string.

And yeah - I output that string to CCLabelTTF - there’s no any rubish in my string, but there’s a rubbish in CCLog.