-3

I'm using the VCL tools in C++Builder 11 for Windows desktop development. I am trying to get the C functions opendir and readdir to work in a 64-bit app. I have read the IBM website (link below) about the 64-bit version of opendir and readdir but I can't configure my code to work with the 64-bit versions. The code below shows a single button app with code that reads and displays the name of each file in a folder. This works as a 32-bit app. On the 64-bit platform this code fails in the while loop calling readdir. Can you show how to adjust this code so it works on the 64-bit VCL platform in C++Builder.

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <dirent.h>
#include <System.SysUtils.hpp>


//-----------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//----------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  DIR *pDir;
  struct dirent *dirU;

  pDir = opendir( "C:\\test\\" ); 
  if (pDir == NULL) {
    ShowMessage("error");
  }else{
    int count = 1;
    while ((dirU = readdir(pDir)) != NULL) //fails here
    {
      ShowMessage(dirU->d_name);
      count++;
    }
  }
}
//------------------------

IBM 64 bit opendir and readdir

phuclv
  • 37,963
  • 15
  • 156
  • 475
homebase
  • 713
  • 1
  • 6
  • 20
  • 2
    Please do not spam the tags, this has nothing with `c` – Pablo Nov 05 '22 at 02:49
  • 1
    `On the 64-bit platform this code fails in the while loop calling readdir.` how does it fail? What's the output? What's the value of `errno`? What does `GetLastError()` return? – phuclv Nov 06 '22 at 03:06

1 Answers1

0

I'm at a loss why you are looking at the IBM site, feels like a flashback of a long long time ago. Anyway, why not use native functions like GetFiles? See https://docwiki.embarcadero.com/Libraries/Alexandria/en/System.IOUtils.TDirectory.GetFiles

Mike Versteeg
  • 1,615
  • 14
  • 28
  • calling `GetFiles()` a native function isn't correct, because it's an Embarcadero thing, not a Windows thing. The standard way in Windows is [`FindFirstFile`/`FindNextFile`](https://stackoverflow.com/q/2314542/995714) and the [standard C++ method](https://stackoverflow.com/q/612097/995714) is [std::filesystem::directory_iterator](https://en.cppreference.com/w/cpp/filesystem/directory_iterator) – phuclv Nov 05 '22 at 11:27
  • looking at the IBM site is a must if one is writing code for AIX or zOS, they're still modern things being developed. But `readdir` and `opendir` aren't Windows things and doesn't exist there except with 3rd party support – phuclv Nov 05 '22 at 11:30
  • I have a lot of 32-bit C code that uses opendir and readdir. I can't rewrite it all with FindFirstFile and FindNextFile because the changes would be extensive. The IBM website was one of the only pages discussing opendir64 and readdir64, I was hoping to find a clue about compiling these functions in 64-bit. There is not very much written about using opendir and readdir in a 64-bit compiler. I am surprised there is not a 64-bit solution for these functions. – homebase Nov 05 '22 at 13:09
  • 1
    **Read your compiler's documentation**, not some irrelevant things. Reading AIX documentation doesn't even help Linux or macOS programming, let alone Windows. **Just read Windows and embarcadero documentations**. Stop using POSIX things like `readdir` in Windows and use either Windows API or the standard C++ `std::filesystem` – phuclv Nov 06 '22 at 02:49
  • There are obviously no readdir64 on POSIX platforms with readdir except AIX because `readdir` just works. It's the design of AIX that's silly to require a 32/64-bit distrinction. Therefore only only IBM who wrote AIX has that documentation. Write standard cross-platform code with `std::filesystem` so there's no need to rewrite the code anywhere else, or use `boost::filesystem` in case you're stuck with older C++ versions – phuclv Nov 06 '22 at 02:56
  • Rather than using `Find(First|Next)File()` directly, C++Builder does have `Find(First|Next)()` functions in ``, if you don't want to use `TDirectory::GetFiles()` or `std::filesystem`. There is no good reason whatsoever in using `opendir()`/`readdir()` in C++Builder at all. – Remy Lebeau Nov 07 '22 at 19:06