- 1 year ago
64-bit opendir and readdir in C++Builder
In C++Builder, you can use the FindFirst
, FindNext
, and FindClose
functions from the io.h
header to implement directory traversal and file listing. These functions work with both 32-bit and 64-bit versions of C++Builder.
Here's an example of how you can use these functions to iterate through a directory and list its files:
cpp#include <io.h>
#include <iostream>
#include <string>
int main() {
intptr_t handle;
struct _finddata_t fileData;
std::string directory = "C:\\path\\to\\directory\\*.*";
handle = _findfirst(directory.c_str(), &fileData);
if (handle != -1) {
do {
if (!(fileData.attrib & _A_SUBDIR)) {
std::cout << fileData.name << std::endl;
}
} while (_findnext(handle, &fileData) == 0);
_findclose(handle);
}
return 0;
}
In this example, the _findfirst
function is called with the directory path and a pointer to a _finddata_t
structure. The function returns a handle that can be used to traverse the directory. The do-while
loop then iterates through the directory entries using _findnext
, printing the names of the files.
Make sure to replace "C:\\path\\to\\directory\\*.*"
with the actual path to the directory you want to list.
Note that the _findfirst
and _findnext
functions work with ANSI strings. If you are working with Unicode or wide strings, you can use the wide-character versions of these functions (_wfindfirst
, _wfindnext
) and the struct _wfinddata_t
structure.
Remember to include the necessary header files (io.h
in this case) and link against the appropriate libraries when compiling your C++Builder project.