Vineel Kumar Reddy
Vineel Kumar Reddy

Reputation: 4716

FindFirstFile not working as expected

FindFirstFile win32 function failing. please resolve the issue. Thanks

#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;
}

Upvotes: 0

Views: 4337

Answers (4)

Droopy
Droopy

Reputation: 124

In the future, anyone that uses preprocessor definitions for API calls and not their direct notation:

#define FindFirstFile FindFirstFileA/W

Should use the preprocessor typedef as a parameter

typedef LPCTSTR LPC(W)STR

TCHAR's are invoked in the WINAPI for a reason people! If you are writing code to be portable in anyone's IDE (pasting code on github for example). Use TCHAR's, LPCTSTR will work with both FindFirstFileA/W.

Or simply use the explicit API notation, FindFirstFileW (in this circumstance as you're expecting system file names, and if they have unicode characters, and the default system charset isn't set to the correct code base, you will get "junk/garbage" characters in your output buffer).

Another side note, if you're working with System file or System Paths, always use Unicode because ANSI relies directly on the code base, and if you're on an arabic system, the file path will be messed up in ANSI.

Upvotes: 0

Digvijay Rathore
Digvijay Rathore

Reputation: 126

you are using char* as a parameter. First you need to convert it to wide char than go for FindFirstFile and FindNextFile Function.

void MergeFile(char * s_Path){

     wchar_t *w_Path = (wchar_t *)malloc(strlen(s_Path) * sizeof(wchar_t));

     mbstowcs(w_Path,s_Path,strlen(s_Path) + 1);

     WIN32_FIND_DATA fd;

     LPCWSTR m_Path = w_Path;

     memset(&fd,0,sizeof(WIN32_FIND_DATA));

     HANDLE hfile = FindFirstFile(m_Path,&fd);

     while(hfile != INVALID_HANDLE_VALUE)
     {

        if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
        {

           printf("\n %S",fd.cFileName);

        }

        if(FindNextFile(hfile,&fd)==false) break;

    }

}

Upvotes: 0

zaknotzach
zaknotzach

Reputation: 1005

If you are having issues with ascii to unicode then you can use another windows API to convert before you pass into the function. This worked perfectly in my C application when I was trying to use FindFirstFile with an ascii path (code simplified for explanation):

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

char searchName[_MAX_PATH];
char resultName[_MAX_PATH];
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
WCHAR wSearchName[_MAX_PATH];

// the path is made from inputs, looks like "path\to\folder\*.extension"
// the path is stored in searchName

// encode string for search
MultiByteToWideChar(CP_UTF8, 0, searchName, _MAX_PATH, wSearchName, _MAX_PATH);

hFind = FindFirstFile(wSearchName, &FindFileData);
while (hFind != INVALID_HANDLE_VALUE)
{
    //convert result name back to ascii and do something with it
    WideCharToMultiByte(CP_UTF8, 0, FindFileData.cFileName, _MAX_PATH, resultName, _MAX_PATH, NULL, NULL);
    // do something with result

    // keep moving to next files
    if (!FindNextFile(hFind, &FindFileData))
    {
        FindClose(hFind);
        hFind = INVALID_HANDLE_VALUE;
    }
}

Upvotes: 1

Vineel Kumar Reddy
Vineel Kumar Reddy

Reputation: 4716

The Problem was resolved my project is set to UNICODE and I am passing a asci string so FindFirstFile is failing. THanks to your suggestions

Upvotes: 1

Related Questions