FindFirstFile win32函數失敗。請解決問題。由於FindFirstFile按預期工作
#include<Windows.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define ValidDir(data) strcmp(data.cFileName,".")&&strcmp(data.cFileName,"..")
void MergeDir(char *path)
{
WIN32_FIND_DATA data={0};
HANDLE h;
int nFiles,i=0;
char temp[MAX_PATH];
char **files = malloc(sizeof(char*)*10000);
sprintf(temp,"%s*.*",path);//path = c:\windows name = *.*
h = FindFirstFile(temp,&data);//temp = c:\windows\*.*
/Invalid handle is being returned.....
if(h != INVALID_HANDLE_VALUE){ //Check whether we got valid handle or not
i = 0;
do{
if(ValidDir(data)){//Checks whether the Dir is . or ..
files[i] = malloc(sizeof(char)*MAX_PATH);
sprintf(files[i++],"%s%s",path,data.cFileName);
}
}while(FindNextFile(h,&data));
nFiles = --i;
FindClose(h);
for(i = 0; nFiles > 0 && i < nFiles-1; i++){
printf("\n%s\n%s\n----------------",files[i],files[i+1]);
}
}
}
int main()
{
//list all files in the following directory
MergeDir("D:\\IIIT\\Sem2\\IRE\\Processed\\");
return 0;
}
也許你可以解釋它爲什麼失敗,任何錯誤消息等等。 – 2011-01-27 19:50:53
如果「無效句柄正在返回」是你的問題,調用GetLastError(),你會知道它爲什麼失敗。 – nos 2011-01-27 19:56:33