c++fileflascc

My C file_exists Function Using stat Always Fails with errno = ENOENT


I'm using FlasCC+Swig to call my C function in Adobe Air application.

The C function:

int file_exists(const char filename[]) {
    struct stat stbuf;
    if (stat(filename, &stbuf) == -1) {
        return (0);
    }
    return (1);
}

In my normal command line (of course, first compile a main.cpp), if I supply "/tmp/test.txt" (the file exists), the function file_exists returns 1, which is expected.

However, after I use FlasCC+Swig to compile this C function file_exists, and generate a swc library, and add this library to my Adobe Air application, and run it, it always returns 0, which is incorrect.

I checked the errno, it's value is ENOENT (2):

A component of path does not name an existing file or path is an empty string.

http://pubs.opengroup.org/onlinepubs/7908799/xsh/stat.html

Can you give me some idea?

BTW, in my Adobe Air application:

var file:File = File.applicationDirectory.resolvePath(path);
trace(file.exists); // returns true

So the Adobe Air DOES have access to the local file system.


Solution

  • It turns out that the low level c code won't be able to see the REAL local file system. We need to use the Adobe Virtual File System.

    CModule.vfs.addDirectory("/MyPath/");
    CModule.vfs.addFile("/MyPath/MyFile.txt", data);
    

    After we set up the above VFS, our C file reading function will be able to see the file /MyPath/MyFile.txt.