谁能帮我确定是什么导致了这里的无限循环?

问题描述 投票:0回答:0

通过 WinSCP 在 shell 上运行这个程序,直到我将这个 while 循环添加到代码中,我才得到一个无限循环:

#ifndef TERMINAL_HPP
#define TERMINAL_HPP

#include <iostream>
#include <sstream>
#include <vector>
#include "filesystem.hpp"

class Terminal {
private:
    FileSystem fs;

public:
    Terminal() {}

    void run() {
        std::string command;
        std::vector<std::string> tokens;

        std::cout << "Welcome to the Terminal!\n";
        while (true) {
        std::cout << fs.pwd() << "$ ";
        std::getline(std::cin, command);

        if (command.substr(0, 4) == "mkdir") {
            std::string name = command.substr(5);
            std::cout << fs.mkdir(name) << std::endl;
        } else if (command.substr(0, 5) == "touch") {
            std::string name = command.substr(6);
            std::cout << fs.touch(name) << std::endl;
        } else if (command == "pwd") {
            std::cout << fs.pwd() << std::endl;
        } else if (command == "ls") {
            std::cout << fs.ls() << std::endl;
        } else if (command.substr(0, 2) == "cd") {
            std::string name = command.substr(3);
            std::cout << fs.cd(name) << std::endl;
        } else if (command.substr(0, 2) == "rm") {
            std::string name= command.substr(3);
            std::cout << fs.rm(name) << std::endl;
        } else if (command == "exit") {
            std::cout << "Exiting..." << std::endl;
            break;
        } else {
            std::cout << "Unknown command. Type 'help' for available commands." << std::endl;
        }
    }


    }
};



#endif /* TERMINAL_HPP */

为了增加参考,我还将包含 FileSystem 类:

#ifndef FILESYSTEM_HPP
#define FILESYSTEM_HPP

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include "node.hpp"

using namespace std;

class FileSystem {

private:
    Node *root;
    Node *currentDirectory;

    Node *findNode(std::string name) {
        std::vector<Node*> children = currentDirectory->getChildren();
        for (Node *node : children) {
            if (node->getName() == name) {
                return node;
            }
        } return nullptr;
    }

public:
    FileSystem() {
        root = new Node("/root", 'd');
        currentDirectory = root;
    }

    ~FileSystem(){
    vector<string> names;
    for(Node *node : root->getChildren()){
      names.push_back(node->getName());
    }
    for (string name : names) {
      root->removeChild(name);
    }
    delete root;
}


    std::string mkdir(std::string name) {
        if (name.empty()) {
            return "mkdir: missing operand";
        }
        if (findNode(name)) {
            return "Error: " + name + " exists";
        }
        Node *node = new Node(name, 'd');
        currentDirectory->addChild(node);
        return "directory " + name + " created successfully";
    }

    std::string touch(std::string name) {
        if (name.empty()) {
            return "touch: missing operand";
        }
        if (findNode(name)) {
            return "Error: " + name + " exists";
        }
        Node *node = new Node(name, 'f');
        currentDirectory->addChild(node);
        return "file " + name + " created successfully";
    }

    std::string pwd() {
    std::vector<std::string> pathStack;
    Node *node = currentDirectory;
    while (node != root) {
        pathStack.push_back(node->getName());
        node = node->getParent();
    }
    std::string path = "/root/";
    while (!pathStack.empty()) {
        path += pathStack.back() + "/";
        pathStack.pop_back();
    }
    path.pop_back();
    return path;
}

    std::string ls() {
    std::string result = "";
    std::vector<Node*> children = currentDirectory->getChildren();
    for (Node *node : children) {
        if (node->getType() == 'f') {
            result += "f " + node->getName() + "\n";
        } else if (node->getType() == 'd') {
            result += "d " + node->getName() + "\n";
        }
    }
    return result;
}

    std::string rm(std::string name) {
    if (name.empty()) {
      return "rm: missing operand";
    }
    Node *node = findNode(name);
    if (node == nullptr) {
      return "No such file or directory";
    }
    if (node->getType() == 'd') {
      // delete directory and all of its contents
      std::vector<Node*> children = node->getChildren();
      for (Node *child : children) {
        rm(child->getName());
      }
    }
    node->getParent()->removeChild(name);
    delete node;
    return name + " removed successfully";
}


    std::string mv(std::string from, std::string to) {
    if (from.empty() || to.empty()) {
      return "mv: missing operand";
    }
    Node *fromNode = findNode(from);
    if (!fromNode) {
      return "file not found";
    }
    Node *toNode = findNode(to);
    if (toNode && fromNode->getType() != toNode->getType()) {
      return "mv: cannot overwrite '" + to + "' with '" + from + "': Different file type";
    }
    fromNode->getParent()->removeChild(from);
    fromNode->setName(to);
    if (toNode) {
      delete toNode;
    }
    currentDirectory->addChild(fromNode);
    return "file/dir renamed successfully";
  }


    std::string cd(std::string name) {
        if (name == "..") {
            if (currentDirectory == root) {
                return "can't change to directory ..";
            } else {
                currentDirectory = currentDirectory->getParent();
                return pwd();
            }
        } else {
            Node *node = findNode(name);
            if (node == nullptr) {
                return name + ": no such directory";
            } else if (node->getType() == 'f') {
                return name + ": is not a directory";
            } else {
                currentDirectory = node;
                return pwd();
            }
        }
    }

};

#endif

每次我运行 ./main 它都会显示“欢迎使用终端!”然后冻结,好像它是无限循环和服务器外壳超时。除了 while 循环之外,main 运行良好,所以我知道它必须是 while 循环中的东西。

c++ while-loop infinite-loop
© www.soinside.com 2019 - 2024. All rights reserved.