Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ - Having problems with a simple example of FindFirstFile

Tags:

c++

I'm using the following ultra-super-mega simple code to list all the files in a direcory (Windows 8.1, Visual Studio Express 2013, C++):

#include <stdlib.h>
#include <stdio.h>
#include <sys/stat.h>

#include <limits>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <bitset>

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


using namespace std;


void get_file_list(string DATA_DIR)
{
    HANDLE hFind;
    WIN32_FIND_DATA data;

    hFind = FindFirstFile(LPCWSTR(DATA_DIR.c_str()), &data);

    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            printf("%s\n", data.cFileName);
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }
}

int main(int argc, char** argv)
{

    string DATA_DIR = "D:\\drobpox\\Dropbox\\BinaryDescriptors\\LFW\\DATA\\*.*";
    //string DATA_DIR = "c:\\Users\\GilLevi\\Downloads\\GraphsSURF\\GraphsSURF\\bark\\*.jpg";
    string OUT_DIR = "D:\\drobpox\\Dropbox\\BinaryDescriptors\\LFW\\LATCH_TXT_FILES\\LATCH8";


    get_file_list(DATA_DIR);

}

However, I "hFind" always equals "INVALID_HANDLE_VALUE". I double checked the path and tried various different paths.

Might the reason be that I'm running a 64bit application and using WIN32_FIND_DATA ?

Thanks in advance, Gil

like image 423
GilLevi Avatar asked Jan 09 '23 14:01

GilLevi


1 Answers

Converting a string to a widestring requires you to allocate memory and use string conversion functions. If you don't want to change the function, the easiest solution is probably to use the non-unicode version of FindFirstFile, by Adding a A to the functionname and struct;

WIN32_FIND_DATAA data;

hFind = FindFirstFileA(DATA_DIR.c_str(), &data);
like image 132
wimh Avatar answered Jan 27 '23 23:01

wimh