Opening all Bitmap files in a single directory
First off, to make things easier, lets assume all the files are in the same directory as the executable. Does anyone know how to search for all Bitmap files of unspecified names in a directory?
ok, Im being a little presumptuous here, but Im assuming C++, Windows, VS.NET
WIN32_FIND_DATA wfd;
HANDLE hf= FindFirstFile("*.bmp", &wfd);
if (hf != INVALID_HANDLE_VALUE)
{
do
{
// name of file stored in wfd.cFileName
} while (FindNextFile(hf, &wfd));
FindClose(hf);
}
WIN32_FIND_DATA wfd;
HANDLE hf= FindFirstFile("*.bmp", &wfd);
if (hf != INVALID_HANDLE_VALUE)
{
do
{
// name of file stored in wfd.cFileName
} while (FindNextFile(hf, &wfd));
FindClose(hf);
}
#ifdef WIN32#include <windows.h>#else#include <dirent.h>#endif#include <iostream>#include <vector>#include <string>using namespace std;namespace file {void getlist (std::vector<std::string>& list, std::string dir, std::string extension){#ifdef WIN32 WIN32_FIND_DATA findData; HANDLE fileHandle; int flag = 1; std::string search ("*."); if (dir == "") dir = "."; dir += "/"; search = dir + search + extension; // SetCurrentDirectory(dir.c_str()); fileHandle = FindFirstFile(search.c_str(), &findData); if (fileHandle == INVALID_HANDLE_VALUE) return; while (flag) { list.push_back(findData.cFileName); flag = FindNextFile(fileHandle, &findData); } FindClose(fileHandle);#else std::string search("."); search += extension; DIR* directory = opendir(dir.c_str()); dir += "/"; struct dirent* entry; std::string temp; std::string final_name; while ((entry = readdir(directory)) != NULL) { temp = entry->d_name; if (temp.find(search, 0) != string::npos) { final_name.clear(); final_name = dir + temp; list.push_back(final_name); } } closedir(directory);#endif}} // end namespace file
Both of those samples are incorrect. FindNextFile() does not return 0 when there are no more files - it returns ERROR_NO_MORE_FILES.
Actually I just "Googled" WIN32_FIND_DATA, reading the descriptions of the functions involved at MSDN I came across the fallowing...
FindNextFile
The FindNextFile function continues a file search from a previous call to the FindFirstFile or FindFirstFileEx function.
BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);
Parameters
hFindFile
[in] Search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.
lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.
The structure can be used in subsequent calls to FindNextFile to see the found file or directory.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
FindNextFile
The FindNextFile function continues a file search from a previous call to the FindFirstFile or FindFirstFileEx function.
BOOL FindNextFile(
HANDLE hFindFile,
LPWIN32_FIND_DATA lpFindFileData
);
Parameters
hFindFile
[in] Search handle returned by a previous call to the FindFirstFile or FindFirstFileEx function.
lpFindFileData
[out] Pointer to the WIN32_FIND_DATA structure that receives information about the found file or subdirectory.
The structure can be used in subsequent calls to FindNextFile to see the found file or directory.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero (0). To get extended error information, call GetLastError.
If no matching files can be found, the GetLastError function returns ERROR_NO_MORE_FILES.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement