[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

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
0
Egore
Asked:
Egore
  • 4
  • 4
  • 2
1 Solution
 
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
 
KangaRooCommented:
>> In windows you should use the windows API functions
Only with VC
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now