Hi,
i want to copy some files from APK to FileUtils::getInstance()->getWritablePath(). To organize them i want to create few directories . Is there a way to do it? I did find anything so far.
Hi,
i want to copy some files from APK to FileUtils::getInstance()->getWritablePath(). To organize them i want to create few directories . Is there a way to do it? I did find anything so far.
@lukasz.b
maybe it can help you for read and sava you data
string TDInvFileUtils::getFileByName(string pFileName){
//get first path
string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;
CCLOG("path = %s",path.c_str());
//create a file point
FILE* file = fopen(path.c_str(), "r");
if (file) {
char* buf; //get string
int len; //get string length
/*获取长度*/
fseek(file, 0, SEEK_END); //move to end
len = ftell(file); //get length
rewind(file); //return to begin
CCLOG("count the file content len = %d",len);
//distribute buff space
buf = (char*)malloc(sizeof(char) * len + 1);
if (!buf) {
CCLOG("malloc space is not enough.");
return NULL;
}
//read file
int rLen = fread(buf, sizeof(char), len, file);
buf[rLen] = '\0';
CCLOG("has read Length = %d",rLen);
CCLOG("has read content = %s",buf);
string result = buf;
fclose(file);
free(buf);
return result;
}
else
CCLOG("open file error.");
return NULL;
}
bool TDInvFileUtils::saveFile(char *pContent, string pFileName){
//get save path
string path = CCFileUtils::sharedFileUtils()->getWriteablePath() + pFileName;
CCLOG("wanna save file path = %s",path.c_str());
//create file point
//path,modle
FILE* file = fopen(path.c_str(), "w");
if (file) {
fputs(pContent, file);
fclose(file);
}
else
CCLOG("save file error.");
return false;
}
if you want to copy file to:
CCFileUtils::sharedFileUtils()->getWriteablePath() + “filename.txt”;
everything works fine.
But if you want to do the same for:
CCFileUtils::sharedFileUtils()->getWriteablePath() + “/sampleDir/filename.txt”;
FILE* file = fopen(path.c_str(), “w”)
returns null because “/sampleDir/” dont exits in this location.
Yeeep!
I just arrived to this point.
We need the create directory method in FileUtils
If you use the latest from GitHUB you can do this with new features, see: https://github.com/cocos2d/cocos2d-x/blob/v3/CHANGELOG
Awesome thanks!