gpt4 book ai didi

c++ - _finddata_t、_findfirst、_findnext 适用于 Mac OS-X

转载 作者:太空狗 更新时间:2023-10-29 21:39:32 28 4
gpt4 key购买 nike

我正在尝试使用 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;
}

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

错误:变量的类型“struct _finddata_t”不完整

我假设这是一个仅限 Windows 的结构,用于 _findfirst 和 _findnext我可以使用什么等效结构和函数在 OS-X 上编译它?

最佳答案

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

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


* 搜索时,我还找到了fdopendir()dupfd() .它们相当新颖,也很有趣。

关于c++ - _finddata_t、_findfirst、_findnext 适用于 Mac OS-X,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32784242/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com