简体   繁体   English

为什么这会陷入无限循环?

[英]Why does this go into an infinite loop?

Okay, so I'm trying to create a list of he folders, and sub-folders and their files, but right now it doesn't print anything, and seems to be going into an infinite loop. 好的,所以我正在尝试创建他的文件夹,子文件夹及其文件的列表,但是现在它无法打印任何内容,并且似乎陷入了无限循环。 Any idea why? 知道为什么吗?

 //infinate loop start
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            dirs.push_back(Directory(dirs[i].folders[j]));

            getfiles(dirs[i].dir,dirs[i].files);
            getfolders(dirs[i].dir,dirs[i].folders);
        }
        //infinate loop end

Here is the full source: 这是完整的源代码:

#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

using namespace std;

struct Directory{
  public:
        int indent;
        vector<string> files;
        vector<string> folders;
        string dir;
        Directory(string mydir){ dir = mydir;}
};

int getfolders (string dir, vector<string> &folders)
{
    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(S_ISDIR(st.st_mode)){
            if(dirp->d_name[0] != '.')
                folders.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

/*function... might want it in some class?*/
int getfiles (string dir, vector<string> &files)
{

    DIR *dp;
    struct stat st;
    struct dirent *dirp;
    if((dp  = opendir(dir.c_str())) == NULL) {
        cout << "Error(" << errno << ") opening " << dir << endl;
        return errno;
    }

    while ((dirp = readdir(dp)) != NULL) {
        stat(dirp->d_name, &st);

        if(!S_ISDIR(st.st_mode)){
            files.push_back(string(dirp->d_name));
        }
    }
    closedir(dp);
    return 0;
}

int main()
{
    struct Directory root = Directory(".");


    vector<string> display = vector<string>();

    cout << "hello\n";

    getfiles(root.dir,root.files);
    getfolders(root.dir,root.folders);

    cout << "hello\n";

    vector<Directory> dirs = vector<Directory>();

    for(int i = 0; i < (int) root.folders.size(); i++){
        dirs.push_back(Directory(root.folders[i]));

       getfiles(dirs[i].dir,dirs[i].files);
        getfolders(dirs[i].dir,dirs[i].folders);


        //infinate loop start
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            dirs.push_back(Directory(dirs[i].folders[j]));

            getfiles(dirs[i].dir,dirs[i].files);
            getfolders(dirs[i].dir,dirs[i].folders);
        }
        //infinate loop end
    }

    cout << "hello\n";

    for (int i = 0; i < (int) root.folders.size();i++) {
        cout << root.folders[i] << endl;
        for(int j = 0; j < (int) dirs[i].folders.size(); j++){
            cout << dirs[i].folders[j] << endl;
        }
    }
    return 0;
}
for (int i = 0; i < (int) root.folders.size();i++) {
    cout << root.folders[i] << endl;
    for(int j = 0; j < (int) dirs[i].folders.size(); i++){
        cout << dirs[i].folders[j] << endl;
    }
}

I don't know much about C++, but shouldn't the second i++ be j++ ? 我对C ++不太了解,但是第二个i++不应该是j++吗? Otherwise j would always be zero and thus satisfy the condition of being less than dirs[i].folders.size() , so the loop could never end. 否则, j将始终为零,从而满足小于dirs[i].folders.size() ,因此循环永远不会结束。

The first for loop in main, on line 78, keeps adding files to root.files. main中的第一个for循环在第78行,一直将文件添加到root.files。 On line 81, 在第81行,

getfiles(root.dir,root.files);

Adds the files to root.files. 将文件添加到root.files。 The for loops stops when i is bigger than root.files.size(), but because this size is increased in every iteration, it never stops. 当我大于root.files.size()时,for循环会停止,但是由于此大小在每次迭代中都会增加,因此永远不会停止。

for(int i = 0; i < (int) root.folders.size(); i++){

The root.folders.size() is evaluated on every loop, and keeps increasing. root.folders.size()在每个循环中都会求值,并且会不断增加。

You should try using a debugger, like gdb, so that you can see what the code does. 您应该尝试使用调试器,例如gdb,以便可以看到代码的作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM