[physfs] Why not search program abs path when add archives?

Thorbjørn Lindeijer bjorn at lindeijer.nl
Tue Nov 27 10:47:36 EST 2012


On Tue, Nov 27, 2012 at 3:14 PM, fy <fy0748 at gmail.com> wrote:
> My code:
>
>       PHYSFS_init(argv[0]);
>       PHYSFS_addToSearchPath("Content/Audio.zip", 1);
>       if (PHYSFS_exists("bgm1.mp3")) {
>           cout<<"test"<<endl;
>       }
>
> If I start my program when current work directory is the program's path,
> it's OK, and print "test".
>
> But if not, it didn't work.
>
>
> So I changed my code like this:
>
>       char *path = getPath();
>
>       PHYSFS_init(path);
>       PHYSFS_addToSearchPath("Content/Audio.zip", 1);
>       if (PHYSFS_exists("bgm1.mp3")) {
>           cout<<"test"<<endl;
>       }
>
> It still didn't work, but i hope get a "true answer".
>
> thx for the lib.

Who says the program's executable path is relevant for resolving
relative file names? This depends on the functionality you're looking
to implement. It is common for file operations to work relative to the
"current working directory", and PhysFS follows that convention
(possibly inheriting it from the standard system file access
functions).

C++ does not provide a standard way to obtain the path of the
executable, but fortunately PhysFS provides a cross-platform
abstraction for this. So your solution would look something like:

    PHYSFS_init(argv[0]);

    std::string audioFile = std::string(PHYSFS_getBaseDir()) +
"/Content/Audio.zip";

    PHYSFS_addToSearchPath(audioFile.c_str(), 1);
    if (PHYSFS_exists("bgm1.mp3")) {
        cout<<"test"<<endl;
    }

See http://icculus.org/physfs/docs/html/physfs_8h.html#a0034c327dfb381a1c95deb80878bcede

Best regards,
Bjørn


More information about the physfs mailing list