Ninjakreborn Posted January 26, 2009 Share Posted January 26, 2009 I found some online sample code that I was going to play with: /* * A function to list all contents of a given directory * author: Danny Battison * contact: gabehabe@hotmail.com */ #include <dirent.h> // directory header #include <cstdio> void listdir (const char *path) { // first off, we need to create a pointer to a directory DIR *pdir = NULL; // remember, it's good practice to initialise a pointer to NULL! pdir = opendir (path); // "." will refer to the current directory struct dirent *pent = NULL; if (pdir == NULL) // if pdir wasn't initialised correctly { // print an error message and exit the program printf ("\nERROR! pdir could not be initialised correctly"); return; // exit the function } // end if while (pent = readdir (pdir)) // while there is still something in the directory to list { if (pent == NULL) // if pent has not been initialised correctly { // print an error message, and exit the program printf ("\nERROR! pent could not be initialised correctly"); return; // exit the function } // otherwise, it was initialised correctly. let's print it on the console: printf ("%s\n", pent->d_name); } // finally, let's close the directory closedir (pdir); } /** EXAMPLE USAGE **/ int main () { listdir ("C:\\"); return 0; } Basically this throws an error about cannot find header file dirent.h. I tried to look this up online and it's a valid class and should be available. I added that include. I am using Visual C++ 2008...and I have the project created and the cpp file created (I have made a bunch of test apps before and they worked). the only error I am recieving is cannot find header file. Any advice is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/142410-direnth-not-including-c/ Share on other sites More sharing options...
corbin Posted January 26, 2009 Share Posted January 26, 2009 Hate to burst your bubble, but I think dirent.h is unix/linux. There might be a Windows version though. If it says it can't find the file, it means it lol. It's not like it's thinking, "Bleh.... I don't feel like loading that file." If I'm wrong and it is a Windows compatible header, make sure the folder that contains it is in the include path for the VS project. Edit: Looks like http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx (FindFirstFile) is a Windows alternative. You would also need to look up FindNextFile. There's even a script to list files in a directory http://msdn.microsoft.com/en-us/library/aa365200(VS.85).aspx. If you're trying to preserve portability, you could always define a function in an #if block. The linux function would rely on opendir/readdir, and the Windows function would rely on FindFirstFile/FindNextFile. Quote Link to comment https://forums.phpfreaks.com/topic/142410-direnth-not-including-c/#findComment-746262 Share on other sites More sharing options...
corbin Posted January 26, 2009 Share Posted January 26, 2009 Blerh to late to edit. Actually, you could probably just define opendir as FindFirstFile in some situations since they seem to have similar usage. Could be wrong though since I didn't check the param list of opendir. Quote Link to comment https://forums.phpfreaks.com/topic/142410-direnth-not-including-c/#findComment-746286 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.