适用于Mac OS-X的_finddata_t,_findfirst,_findnext

时间:2015-09-25 14:17:16

标签: c++ macos gcc

我正在尝试使用CodeBlocks和GCC编译器在OS-X上编译程序:

    extern int listFiles(void) {
            struct _finddata_t myFile;
            int hFile;

            if ((hFile = _findfirst("*.txt", &myFile)) == -1L) {
                printf("No text files in current directory.\n");
                return 0;
            }

            printf("File name: %s   File size: %d", myFile.name, myFile.size);

            while (_findnext(hFile, &myFile) == 0) {
                printf("File name: %s   File size: %d", myFile.name, myFile.size);
            }

            _findclose(hFile);
            return 1;
    }

但是,编译失败并出现此错误:

error: variable has incomplete type 'struct _finddata_t'

我假设这是一个仅限Windows的结构,用于_findfirst and _findnext我可以使用哪些等效的结构和功能在OS-X上编译?

1 个答案:

答案 0 :(得分:2)

作为Igor Tandetnik commented,您应该查看函数opendir()readdir()closedir() * 以及DIR *struct dirent类型。这是读取目录中每个条目的基本低级接口,以不确定的顺序显示名称(实际上,它通常是名称出现在原始目录中的顺序,但通常不是字母或任何其他顺序)没有任何过滤。

如果您想过滤文件名以便只选择.txt个文件,那么您必须转到POSIX的更加模糊的部分:scandir()。这允许您提供一个选择器函数(回调),告诉scandir()何时返回给定条目,并且还使用比较器函数来对名称列表进行排序(就像通过qsort()一样)。

<小时/> * 在搜索时,我还找到了fdopendir()dupfd()。它们相当新颖而且非常有趣。

相关问题