Hi,
I read this page about reading and writing to a file.
http://www.cocos2d-x.org/projects/cocos2d-x/wiki/How_to_read_and_write_file_on_different_platforms
I’m more confused now than before I read it.
It suggests that you use CCFileUtils::getWriteablePath() to get the path, I assume this is the correct path for all platforms.
It then gives examples that are different for the 3 platforms that I use (IOS, Android and Win32).
For IOS it suggests creating a FileOperation class in the ios folder (not classes) (also, the FileOperation.h is missing in screenshot).
For both Android and Win32 it suggests creating the class in the Classes folder (which makes more sense).
If you look in the example classes, FileOperation has its own getFilePath function, this does not use CCFileUtils::getWriteablePath.
The write and read functions are the same in the .mm and .cpp version of the files, only getFilePath is different.
can I just do this:-
void MyClass::saveFile(string path)
{
FILE *fp = fopen(path.c_str(), "w");
if (! fp)
{
CCLOG("can not create file %s", path.c_str());
return;
}
fputs("file example", fp);
fclose(fp);
}
void MyClass::readFile(string path)
{
FILE *fp = fopen(path.c_str(), "r");
char buf[50] = {0};
if (! fp)
{
CCLOG("can not open file %s", path.c_str());
return;
}
fgets(buf, 50, fp);
CCLOG("read content %s", buf);
fclose(fp);
}
And then call readFile and writeFile passing in CCFileUtils::getWriteablePath() + “/myfilename.txt”
Will this work on all platforms?
Should I use CCFileUtils::getWriteablePath() when reading a file?
If the wiki page is giving bad advise, can it be updated?