首页 > 解决方案 > 是否有从 StringStream 读取输入的正确方法

问题描述

我正在编写一个以串行和并行方式执行命令的自定义 bash。当我尝试将任何内容回显到终端时,行尾会附加一个空格,这使我无法通过测试用例。我的进程命令方法中有错误:

Commands commandProcess(std::string line, bool parallel, bool single) {
   // Create a list of commands to be returned if there
   // is processing occurring.
   Commands list; // vector of strings
   // Set up a string stream to read the line
   std::stringstream ss(line);
   std::string cl = ""; // clean line
   // While the string stream is still has unread info read
   // through quotes and assign the commands to a list.
   std::string temp = "";
   while (ss >> std::quoted(temp)) {
       cl += temp + " ";
       list.push_back(temp);
   }
   boost::trim_right(cl);
   std::cout << "Running: " << cl << std::endl;
   // Run a one line argument
   if (!parallel && single) {
       ChildProcess cp;
       cp.forkNexec(list);
       std::cout << "Exit code: " << cp.wait() << std::endl;
   }
   return list;
}

除了 echo 命令,在我的程序中一切正常。非常感谢任何指导。

预期产出

标签: c++c++11vectorstringstream

解决方案


推荐阅读