next up previous contents
Up: Scanning Directories in a Previous: The DirScan Class

 

A Directory Scan Example

The following function prints the name and loid of each entry in the directory given by the pathname argument.

    int count(const char *pathname)
    {
        DirEntry entry;
        int count = 0;

        // Open a scan over the pool given by "pathname."
        DirScan scan(pathname);

        // Make sure the scan was successfully opened.
        if(scan != RCOK){
            cout << "Error scanning directory " << pathname << ": "
                 << rc << endl;
            return 0;
        }

        // Scan until end-of-scan or an error is encountered.
        for(count = 0; scan.next(&entry) == RCOK; ++count);

        cout << path << " has " << count << " objects." << endl;

        // Check for errors.
        if(scan.rc().err_num() != OC_EndOfScan){
            cout << "Error scanning directory " << pathname << ": "
                 << rc << endl;
            return 0;
        }

        // The destructor will close the scan object.

        return count;
    }



Marvin Solomon
Fri Aug 2 13:38:24 CDT 1996