Problems with reading/opening files


#1

Hello, i’m trying to open a file with text lines in it, processing the lines and returning part of them, but when I try to open the file (a .strings) it doesn’t find it.

Here is the code:

        string filename = "english.strings";

    const char * path = cocos2d::CCFileUtils::fullPathFromRelativePath(filename.c_str());

    FILE * myfile;
    myfile = fopen (path,"r");

    char *buffer;
    string *returnValue = new string();

    if (myfile!=NULL)
    {
        //get the lenght of the file
        fseek (myfile , 0 , SEEK_END);
        long lSize = ftell (myfile);
        rewind (myfile);

        buffer = new char [lSize];
        fread (buffer,1,lSize,myfile);
        fclose(myfile);

        string res = parseFileData(buffer, text);
        delete (buffer);

        if (res.length() == 0)
        {
            returnValue->append(text);
        }
        else
        {
            returnValue->append(res);
        }
    }
    else CCLOG("FILE not found");

    return returnValue->c_str();

the file “english.strings” is in the assets folder and when i print the full path it says “english.strings” as it should If I’m not wrong, but It never finds it (I always get the “FILE not found” text) and it doesn’t crash. I have also tried using “rb” instead of “r” in the fopen()

Do you know what i’m doing wrong?

Also, you say that if we use static unsigned char* getFileData ( . . .) if it’s succesful it will return the pointer and if it failed it will return “NULL” but I checked (i tried to use it in the above case) and the code says

...

 unsigned char * pBuffer = NULL;

    do 
    {
        // read the file from hardware
        FILE *fp = fopen(pszFileName, pszMode);
        CC_BREAK_IF(!fp);

        fseek(fp,0,SEEK_END);
        *pSize = ftell(fp);
        fseek(fp,0,SEEK_SET);
        pBuffer = new unsigned char[*pSize];
        *pSize = fread(pBuffer,sizeof(unsigned char), *pSize,fp);
        fclose(fp);
    } while (0);

....

So it will never return NULL right? Just making sure, because I never got NULL even though the fopen did not find the file…

Thanks!


#2

You can not read a file using fopen() on android, because the files are packed into apk, which is a zip file.
You can use CCFileUtils::getFileData().


#3

but when i need to file check…

ex) i want use hd.png file… in android. but all file is not exist… some of files are just .png…
so… i have to file check… then chaned… file name. to
hd in that time…

if hd file is not exist…
i want to rerun “xxx.png”
if
hd is exis…
return “xxx-hd.png”

so i have check… file exist first…
but CCFileUtils::getFileData() is to hard… cuz it allready read binary data… to. memory…
i need… just check… file…

what i have to do…??

fopne is not working…

my english is bad… sorry