我的C file_exists函数使用stat始终失败并且errno = ENOENT

时间:2014-02-23 03:18:59

标签: c++ file flascc

我正在使用FlasCC + Swig在Adobe Air应用程序中调用我的C函数。

C函数:

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

在我的正常命令行中(当然,首先编译一个main.cpp),如果我提供“/tmp/test.txt”(文件存在),函数file_exists将返回1,这是预期的

然而,在我使用FlasCC + Swig编译此C函数file_exists并生成swc库并将此库添加到我的Adobe Air应用程序并运行它之后,它总是返回0,这是不正确的

我查看了errno,其值为ENOENT (2)

  

路径的组件未命名现有文件或路径为空   字符串。

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

你能给我一些想法吗?

BTW,在我的Adobe Air应用程序中:

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

因此Adobe Air DOES可以访问本地文件系统。

1 个答案:

答案 0 :(得分:0)

事实证明,低级别的c代码将无法看到 REAL 本地文件系统。我们需要使用Adobe虚拟文件系统。

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

在我们设置上述VFS之后,我们的C文件读取功能将能够看到文件/MyPath/MyFile.txt