File exists?

Hi guys, is there any way to check if a file exists before loading? I’ve seen you can call CCFileUtils::getFileData and it will return NULL if the file doesn’t exist, but this will also load the full file if it does exist.

This is fine for methods like CCSprite::spriteWithFile that need to load the data if the file exists. However I simply want to check if a file exists and return a filename if it does or an alternate filename if it doesn’t. Kind of how iOS’s @2x file naming for assets works.

If there is a way to do this please advise. I know how to do it for iOS and Android, but my game has to support other platforms as well which is why I chose cocos2d-x :slight_smile:

Otherwise if I could humbly make a suggestion for a future release… :slight_smile:

Thanks

Hi !

I had the same need, so i make one… for android version :smiley:

bool CCFileUtils::existFileData(const char* pszFileName)
{
    string fullPath(pszFileName);

    if ((! pszFileName))
    {
        return false;
    }

    if (pszFileName[0] != '/')
    {
        // read from apk
        fullPath.insert(0, "assets/");
        return CCFileUtils::existFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str());
    }
    else
    {
        do
        {
            // read rrom other path than user set it
            FILE *fp = fopen(pszFileName, "rb");
            if (fp != NULL)
            {
                fclose(fp);
                return true;
            }
        }
        while (0);
    }
    return false;
}

bool CCFileUtils::existFileDataFromZip(const char* pszZipFilePath, const char* pszFileName)
{
    unzFile pFile = NULL;
    bool res = false;
    do
    {
        CC_BREAK_IF(!pszZipFilePath || !pszFileName);
        CC_BREAK_IF(strlen(pszZipFilePath) == 0);

        pFile = unzOpen(pszZipFilePath);

        int nRet = unzLocateFile(pFile, pszFileName, 1);
        res = UNZ_OK == nRet;

    } while (0);

    if (pFile)
    {
        unzClose(pFile);
    }
    return res;
}

you can use it, if you want :wink: and it can be added in the official release :smiley:

#1060 is created for it.

no need to add this feature.
using this code would meet your need.

std::ifstream f(fullPath);
if(!f) return;  // file not exist. :) 

Thanks very much for posting this Jean-Christophe BOSSU. It would be great to see this function make it’s way into the main library…

Ben

Ren Wang wrote:

no need to add this feature.
using this code would meet your need.
>
[…]

For other people reading this, unfortunately you can’t use this solution on Android (it works fine on other platforms). On Android the resources are packed into a .zip file, so you can’t just grab a normal file handle. Using a combination of these two methods is the best way to ensure platform compatibility.

Ben

Ben Ward wrote:

Ren Wang wrote:
> no need to add this feature.
> using this code would meet your need.
>
> […]
>
For other people reading this, unfortunately you can’t use this solution on Android (it works fine on other platforms). On Android the resources are packed into a .zip file, so you can’t just grab a normal file handle. Using a combination of these two methods is the best way to ensure platform compatibility.
>
Ben

oh. sorry . i just have tried it on android . as you said , did not work on android~ :frowning: ,thank you for pointing out .

Ren Wang wrote:

oh. sorry . i just have tried it on android . as you said , did not work on android~ :frowning: ,thank you for pointing out .

No worries, I assumed it would work as well but it was only upon testing that I discovered otherwise. Thanks very much for posting!

Ben

I’ve modified this method to be compatible with newest cocos.

Please feel free to add it to official release if You want :slight_smile:

bool CCFileUtils::existFileData(const char* pszFileName)
{
    if ((! pszFileName) || 0 == strlen(pszFileName))
    {
        return 0;
    }

    if (pszFileName[0] != '/')
    {
        string fullPath = fullPathForFilename(pszFileName);
        return s_pZipFile->fileExists(fullPath);
    }
    else
    {
        do
        {
            // read rrom other path than user set it
            FILE *fp = fopen(pszFileName, "rb");
            if (fp != NULL)
            {
                fclose(fp);
                return true;
            }
        }
        while (0);
    }

    return false;
}

Adam Pach wrote:

I’ve modified this method to be compatible with newest cocos.
>
Please feel free to add it to official release if You want :slight_smile:

I think it’s already in there. It’s called something like “isFileExist()” I believe…

Ben