1

I come from a C# background and I am working on a C++ project. I need to open files in a directory, then process that data in the files. The problem is on my target environment (Greenhills Integrity), I cannot access a "directory". It seems C++ does not have a concept of a directory. Why not? This problem is simple in C#. I cannot link to any big library(BOOST or dirent) to get the files. I can open a file using fopen, but I won't always know the file names, so I have to "strcat" the directory to each filename in order to "fopen" the files.

I need a way to just get the file names in a directory without using an external API. Is that possible?

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
Blade3
  • 4,140
  • 13
  • 41
  • 57
  • In C++ there is no notion of a directory. The underlying system treats files and directories the same - as "handles." IIRC there is a flag on a filesystem handle which indicates if it's a directory (look at the stat function.) – Andy Shellam Mar 09 '10 at 14:54
  • Added `posix` tag since Greenhills Integrity supports POSIX. – Kirill V. Lyadvinsky Mar 09 '10 at 15:07

3 Answers3

4

The major C++ APIs have directories. Start with readdir on POSIX or FindFirstFile() on Windows. Greenhills seems to support POSIX.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • Maybe someone thought that hints for accomplishing something on Win32 and Unix aren't all that helpful for someone hacking on Greenhills Integrity? Mind you, while it wasn't me, I wouldn't blame them. – sbi Mar 09 '10 at 14:56
  • 1
    I initially down-voted because readdir is not a generic C++ function; however have re-upvoted since the edit to indicate the required function is operating specific. – PP. Mar 09 '10 at 15:00
3

No, it's not possible. C++ has no "built-in" directory functionality - you need to use a library of some sort.

  • Of course it's possible. While C++ doesn't have any specific directory classes (like the file streams) you can just the underlying system calls - readdir et al. http://www.cs.cf.ac.uk/Dave/C/node20.html – Andy Shellam Mar 09 '10 at 14:53
  • 3
    readdir isn't a system call. All I/O in C++ (as in C) is prerformed via libraries. –  Mar 09 '10 at 14:54
2

Check with your operating system. Directory handling is different for each. You will have to use the Windows 32 API if you want to list/query directories on Microsoft Windows, and the Linux API (e.g. opendir/stat) if you want to list/query directories on Linux.

PP.
  • 10,764
  • 7
  • 45
  • 59
  • 1
    See http://stackoverflow.com/questions/883594/microsoft-visual-studio-opendir-and-readdir-how – PP. Mar 09 '10 at 14:52