【问题标题】:findnext() function call fails on second call. Program crashesfindnext() 函数调用在第二次调用时失败。程序崩溃
【发布时间】:2012-07-27 17:29:49
【问题描述】:

_findnext() 函数在第二次调用时不起作用。这是代码。

int main() {
    struct _finddata_t ffblk;
    intptr_t done;

    chdir("tmp");
    printf("Directory changed to dir successfully\n");

    done=_findfirst("*.txt",&ffblk);
    printf("Call to findfirst successful\n");
    printf("%s\n",ffblk.name);
    do{
        done=_findnext(done,&ffblk);
        printf("%s\n",ffblk.name);
    } while(!done);
    printf("Exited the main loop\n");
    getch();
}

程序在获取并打印文件夹tmp 中前两个文件的名称后崩溃。 打印的第一个文件名来自findfirst() 函数。下一个文件名由findnext() 打印。我已经通过调试检查过。

现在当findnext() 在循环中第二次被调用时,程序崩溃了。我正在使用 gcc 并尝试了大多数返回值的东西,但没有成功。有什么想法吗?

【问题讨论】:

  • 什么是_findfirst、_findnext、_find*?我不记得这在 C 标准中。
  • 您是否使用了调试器并检查它在哪一行崩溃?
  • Google 告诉我 _find* 是 Microsoft 的 Visual C++ 的一部分。我建议使用适当的标签进行编辑。
  • 检查文档,_findfirst 显然返回 intptr_t、_findnext 和 int
  • @vish213 - 不。C 标准没有为 16 位编译器规定特殊功能。 C standard 中的任何地方都没有提到findfirst 的_findfirst。

标签: c msvcrt


【解决方案1】:

您的问题是您无法将_findnext 的值分配给您的句柄,您需要两个变量:

intptr_t handle = 0;
int done = 0;

handle = _findfirst("*.txt",&ffblk);
while(handle && done != -1)
{
    printf("%s\n",ffblk.name);
    done = _findnext(handle,&ffblk);
}

【讨论】:

  • _findfirst() 在失败时返回 -1,而不是 0。
  • 等它成功了!!谢谢。我没有更正_findnext 的参数。非常感谢。
猜你喜欢
  • 2015-10-17
  • 1970-01-01
  • 1970-01-01
  • 2018-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2010-12-21
相关资源
最近更新 更多