4

I was writing a C++ code that iterates through all the entries of a directory and decided to use dirent.h. Since I was using C++, I decided to use the C++ standard for including C library files [i.e. prefixing the library name with c and removing the .h at the end]

Without adding any functionality I decided to compile my program once to see if there was actually a cdirent or sys/cstat. g++ threw an error telling me that these files where not present

#include <iostream>
#include <cdirent>
#include <sys/cstat>
#include <sys/ctypes>

using namespace std;

int main()
{

}

dummy.cpp:2:19: error: cdirent: No such file or directory
dummy.cpp:3:21: error: sys/cstat: No such file or directory
dummy.cpp:4:22: error: sys/ctypes: No such file or directory

Following the advice given in this page on where to look for standard C++ header files I could locate cstdio, cstdlib, ctime and the like which brings me to my questions

  1. When is a C standard library file converted to a C++ standard library file?
  2. How should one include such files in the program? The page here only talks about standard and non standard header files. But I believe dirent.h is a standard library file [Correct me if I am wrong]

Thanks in advance for all the suggestions

Community
  • 1
  • 1
rgk
  • 858
  • 16
  • 28
  • 1
    Instead of the Posix files, use Boost filesystem. Boost filesystem was slated for inclusion in the standard library in C++14 (the current standard), but as I recall it wasn't added. It will surely be there in C++17, then with the sillybugs hopefully removed. – Cheers and hth. - Alf Dec 02 '14 at 05:31

1 Answers1

5

dirent.h and sys/stat.h are POSIX/SUS headers rather than standard C/C++ headers, and as such do not adhere to the standard C++ rules.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358