next up previous contents
Up: Scanning Directories in a Application 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 and returns a count of entries.

    int print_dir(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.rc() != RCOK) {
            cout << "Error scanning directory " << pathname << ": "
                 << scan.rc() << endl;
            return 0;
        }

        // Scan until end-of-scan or an error is encountered.
        for(count = 0; scan.next(&entry) == RCOK; ++count)
            cout << entry.name << '\t' << entry.loid << endl;

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

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

        // The destructor will close the scan object.
        return count;
    }
A complete program using this function may be found in the examples/shls directory of the documentation release. Assuming the shell variable $SHROOT points to the root of a copy of the documentation release, you can compile and run the shls example as follows:
    mkdir shls
    cp $SHROOT/examples/shls/* shls
    cd shls
    make shls
    shls
The results will be more interesting if you have something in your Shore file system, for example if you did not clean up after running the tests described in the tutorial Getting Started with Shore .



This page was generated from LaTeX sources
10/27/1997