Developers Manual > Cocos2d-x > Modules > File IO > How does CCFileUtilsfullPathFromRelativePath work

How does FileUtils::fullPathForFilename work

Introduction

This document is based on cocos2d-x 3.1

FileUtils::fullPathFilename() is used to retrieve the full file name from a specified base file name. It returns an absolute path if an existing file is found otherwise it returns the given argument.

Provided that a search path array and resolution array are specified during the initialization, the engine will combine them with the App path into a full path and try to locate a readable file within them. The details of workflow are explained below.

Workflow

Assume that we are running the following codes.

1
2
3
4
5
FileUtils::getInstance()->addSearchPath("Images1");
FileUtils::getInstance()->addSearchPath("Images2");
FileUtils::getInstance()->addSearchResolutionsOrder("iPad");
FileUtils::getInstance()->addSearchResolutionsOrder("iPhone");
std::string fullpath = FileUtils::getInstance()->fullPathForFilename("sprite.png");

And we are placing some files inside Resource directory like this.

Resource\Images1\iPad\house.png
Resource\Images1\iPhone\house.png
Resource\Images2\iPad\house.png
Resource\Images2\iPad\sprite.png
Resource\Images2\iPhone\house.png
Resource\Images2\iPhone\sprite.png

So the engine will do a search in the following order:

Resource\Images1\iPad\sprite.png
Resource\Images1\iPhone\sprite.png
Resource\Images2\iPad\sprite.png
Resource\Images2\iPhone\sprite.png

In our case, line #3 will be hit since an existing file is found on device. And in practice you don't have to care about how and where the resource files are packed. You just use FileUtils::fullPathForFilename() to pick a proper file among those you placed in the Resource directory before.

ios.png (16.6 kB) zhangbin@cocos2d-x.org, 2011-05-09 01:12

win32.png (9 kB) zhangbin@cocos2d-x.org, 2011-05-09 02:01

wophone.png (25.9 kB) zhangbin@cocos2d-x.org, 2011-05-09 02:01

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.