質問

I'm working on an openFrameworks app that will allow me to drag and drop a directory to find all header files. The following function takes the dropped directory and recursively scans it (and all its subdirectories) for header files and pushes them back into the headerFile vector.

At the moment it will only find a fraction of the headers in the original directory, however after doing some cout checks I'm pretty sure I can see that is entering sub-directories. If anyone can tell me what's going wrong I'd be greatly appreciative!

Here's my code!

void Directory::scanDirectory(ofDirectory dir)

{    

int i, size;
ofDirectory* newDir;

size = dir.size();

for (i = 0; i < size; i++){

    if (dir.getFile(i).isDirectory()==1){

        newDir = new ofDirectory(dir.getFile(i).getAbsolutePath());
        newDir->listDir();
        newDir->sort();
        scanDirectory(*newDir);

    }

    else if (dir.getFile(i).getExtension() == "h"){

        headerFiles.push_back(dir.getFile(i));

    }

    i++;

}

}
役に立ちましたか?

解決

EVERYBODY IT'S OK!

JOSHUA PARIS BATTY << has discovered the solution. I'm accidentally iterating over each file twice. I was originally using a while loop and iterating i at the end of it to move through the files. However, I should have taken out the final "i++" once I changed it to a for loop. Woops!

他のヒント

1) size = dir.sizes() should be replaced by size = dir.listDir(); becoz the directory needs to be populated..

2) you have an i++ inside your for loop..causing it wrong loop..

3) no need to use pointer for the newdir..

check my example below...

void Directory::scanDirectory(ofDirectory dir){
int i,size;
size = dir.listDir();
dir.sort();

for (int i = 0; i < size; i++){
if (dir.getFile(i).isDirectory()==1){
ofDirectory newDir(dir.getFile(i).getAbsolutePath());
ofApp:: scanDirectory( newDir );
}else {
fileslist.push_back(dir.getPath(i));
}
}
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top