Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/142410-direnth-not-including-c/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.