I write a c program using Win32 API FindNextFile to find files
#include<stdio.h>
#include<tchar.h>
#include<windows.h>
int _tmain(int argc, TCHAR *argv[])
{
HANDLE hNextFile;
WIN32_FIND_DATA findFileData;
LPCTSTR fileName = argv[1]; //input argument "C:\test\file*.txt"
hNextFile = FindFirstFile(fileName, &findFileData);
while(hNextFile != INVALID_HANDLE_VALUE)
{
printf("long name: %s\t8dot3 name: %s\n", findFileData.cFileName, findFileData.cAlternateFileName);
hNextFile = FindNextFile(fileName, &findFileData); //Unhandled exception here!
}
printf("%s", GetLastError());
return 0;
}
When call FindNextFile firstly, it throw exception. Exception info:
Unhandled exception at 0x77178dc9 in findfile.exe: 0xC0000005: Access violation writing location 0x005c0080.
Could you give me some suggestions?
Thanks in advance.
I have modified my code like this, it works fine. Thanks for Pierre's explain.
#include<stdio.h>
#include<tchar.h>
#include<windows.h>
int _tmain(int argc, TCHAR *argv[])
{
HANDLE hNextFind;
WIN32_FIND_DATA findFileData;
LPCTSTR fileName = argv[1];
BOOL result = TRUE;
if((hNextFind = FindFirstFile(fileName, &findFileData)) == INVALID_HANDLE_VALUE)
return 1;
while(result)
{
_tprintf(TEXT("long name: %s\t8dot3 name: %s\n"), findFileData.cFileName, findFileData.cAlternateFileName);
result = FindNextFile(hNextFind, &findFileData);
}
FindClose(hNextFind);
return 0;
}