请求排查C++终端程序中while循环引发的无限循环问题
问题描述
我通过WinSCP在shell环境运行C++终端程序,添加while循环后,每次执行./main,程序仅显示"Welcome to the Terminal!"便冻结,疑似进入无限循环并导致服务器shell超时;移除该while循环后程序可正常运行,因此问题肯定出在这个while循环中。
Terminal类代码
#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
内容的提问来源于stack exchange,提问作者Todd Foxx
相关产品推荐
相关产品推荐

