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
LVL 3
EgoreAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

KangaRooCommented:
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.
0
nietodCommented:
In windows you should use the windows API functions FindFirstFile() and FindNextFile(). and then FindClose() when you are done.

The FindFirstFile does 2 two things.  First it finds the first file matching your search parameters and returns info about it in WIN32_FIND_DATA structure.  Second it saves the search information in window's memory somewhere and it returns a handle to this search information.  You can then pass this handle onto FindNextFile() so that additional files can be found.  Each time FindNextFile() is called, the information for the next file that matches the search parameters is returned.  When there are no more files found, you call FindClose() which then releases the search information stored by windows.

Let me know if you have any questions.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KangaRooCommented:
>> In windows you should use the windows API functions
Only with VC
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

nietodCommented:
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).
0
KangaRooCommented:
Ok, but dirent.h is not unix specific. Borland and Gcc compilers support them for Windows ?
0
nietodCommented:
I see.  In that case it can probably be ported to VC very easily--probably little or nothing to do.
0
EgoreAuthor Commented:
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?
0
nietodCommented:
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.
0
KangaRooCommented:
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/
0
EgoreAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.