How to get the return of opendir in C++

Nov 27, 2011 at 10:53am
How do I read opendir and search the buffer for the string .png what more is required? heres my attempt.


This is the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
            // first off, we need to create a pointer to a directory
            //Pointer for DIR
            DIR *directorypointer = NULL; // remember, it's good practice to initialise a pointer to NULL!

            //directorypointer will refer to the current directory
            directorypointer = opendir ("folder");

        //if directorypointer does work
        if(directorypointer)
        {
            fprintf(stderr,"Works?");
        }


        //Creating a Struct of Dirent and pointing it to pent
            struct dirent *pent = NULL;

            if (directorypointer == NULL) // if pdir wasn't initialised correctly

            {


            // print an error message and exit the program

            fprintf (stderr,"\nERROR! pdir could not be initialised correctly");



    } // end if


    // while there is still something in the directory to list
    while (pent = readdir (directorypointer))
        {
        // if pent has not been initialised correctly
        if (pent == NULL)

        {

        // print an error message, and exit the program

        printf ("\nERROR! pent could not be initialised correctly");



        }
        // otherwise, it was initialised correctly. let's print it on the console:
        fprintf (stderr,"%s\n", pent->d_name);


    }
    
int buffer[100];
    
int n = NULL;
        
opendir.read(buffer,n);
            


}





Here's my error
main.cpp|563|error: request for member 'read' in 'opendir', which is of non-class type 'DIR*(const char*)'|


This error makes sense but I don't know what else has the class type of read that has the same functionality as opendir has.



Nov 27, 2011 at 11:40am
opendir.read?

Here's a sample directory traversal.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void traverse(const std::string &dirname)
{
        if (DIR *dir = opendir(dirname.c_str()))
        {
                while (struct dirent *entry = readdir(dir))
                {
                        if (strcmp(entry->d_name, ".") == 0)
                                continue;
                        if (strcmp(entry->d_name, "..") == 0)
                                continue;

                        std::string filename = dirname + entry->d_name;
                        if ((entry->d_type & DT_DIR) == DT_DIR && !((entry->d_type & DT_LNK) == DT_LNK))
                        {
                                std::string dirname = filename + "/";
                                traverse(dirname);
                        }
                        else if ((entry->d_type & DT_REG) == DT_REG)
                        {
                                std::cout << filename << std::endl;
                        }
                }

                closedir(dir);
        }
}


If you want to process the files, use an fstream.
Last edited on Nov 27, 2011 at 11:49am
Nov 27, 2011 at 2:44pm
Thanks kbw...
Topic archived. No new replies allowed.