Link to home
Start Free TrialLog in
Avatar of Egore
Egore

asked on

Directory Reading

I have VC++ 6.0 but can't seem to compile a simple chunk of code.  I wrote this in Linux and it compiled just fine with gcc and with's BeOS's BeIDE.  However, VC++ yells at me saying that "dirent.h" cannot be found.

Here's part of the code:

#include <stdio.h>
#include <dirent.h>

...

char *dir;
DIR* dir_struct;
struct dirent* entry;

dir = NULL;
dir = malloc(sizeof(char) * 50));
strcpy(dir, "MyDir");

...

// Open directory
if((dir_struct = opendir(dir)) == NULL)
{
      printf("\nError opening directory structure\n");
      return false;
}
else
{
      while(entry = readdir(dir_struct))
      {
            char fn[5000];

            // Build directory / path string
            strcpy(fn, dir);
            strcat(fn, entry->d_name);
      }
      closedir(dir_struct);
}

...

That's pretty straight-forward -- but dirent.h doesn't exist in Windows.  So, do you know of any similar libraries for Windows that will allow me to retrieve the names of every file and every directory within a directory?

It is preferable if it does not use the Windows API as I want this code to be as portable as possible, but pretty much any method you can think of would be fine.

NOTE:  I don't want this:
...
system("dir > dir.txt");
file = fopen("dir.txt", "r");
...

I really need some sort of C (or C++) routine that will allow me to do this.

Thanks,
Alex
Avatar of KangaRoo
KangaRoo

Can you find anything about directory streams?
It is very likely VC doesn't support this Posix stuff (judging from previous Q). It isn't standard C btw.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> In windows you should use the windows API functions
Only with VC
That, was not clear on my part, the point was you have to use OS-specific code and unix-specific code won't do it.  (Or you can use code that was designed to use conditional compilation so it can run on multiple OSs).
Ok, but dirent.h is not unix specific. Borland and Gcc compilers support them for Windows ?
I see.  In that case it can probably be ported to VC very easily--probably little or nothing to do.
Avatar of Egore

ASKER

Alright, so if it's possible to port it "very simply" what should I do?  Install djgpp or Turbo C++ and compile with one of those?
C++ compilers produced by different manufacturers are almost never compatible.  (only C++ source code is compatible.)  So you will have to get the dirent.h file (and any associated source code, like a dirent.cpp, if there is any) and compile these in VC.
Theoretically, there should be source code for mingw32 and djgpp, its GNU after all ;), but I've never bothered about finding.
GNU    http://www.gnu.org
MINGW  http://agnes.dida.physik.uni-essen.de/~janjaap/mingw32/index.html
       ftp://ftp.xraylith.wisc.edu/pub/khan/gnu-win32/mingw32/
Avatar of Egore

ASKER

Thank you all for the help.  I am definately going to try Turbo C++ and DJGPP to see if they can compile the code as-is.  But, nietod's answer fulfilled the requirements of my question very nicely.

Thanks a lot.