The _findnext()
function is not working when called for the second time. Here is the code.
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();
}
The program crashes once it gets and prints the name of first two files in the folder tmp
.
The first file name printed is from the findfirst()
function. The next file name is printed by findnext()
. I have checked via debugging.
Now when findnext()
is called the second time in the loop, the program crashes. I am using gcc and tried most of the things with return value too, but no success. Any ideas ?
Your problem is that you cannot assign the value of _findnext
to your handle, you need two variables:
intptr_t handle = 0;
int done = 0;
handle = _findfirst("*.txt",&ffblk);
while(handle && done != -1)
{
printf("%s\n",ffblk.name);
done = _findnext(handle,&ffblk);
}