【问题标题】:Error when running shell command in C++ ubuntu linux在 C++ ubuntu linux 中运行 shell 命令时出错
【发布时间】:2017-02-20 22:46:08
【问题描述】:
//all variables are declared in a struct StockPile
//...
string itemid;    
string itemdesc;  
string datepurchased;
string line;
int unitprice;
int totalsales;
std::string myline;
//...

void displaydailyreport() {

    ifstream myfile("stockdatabase.txt");   

    for(int i=0;std::getline(myfile,myline);i++) 
    {
        // Trying to grep all data with a specific date from a textfile,
        cout<<system("grep "<<stockpile[i].datepurchased<<" stockdatabase.txt")<<endl;
    }   
    cout<<endl; 
}

当我尝试编译时,它给了我这个错误:

note:template argument deduction/substitution failed:
Main.cpp:853:40: note:   mismatched types ‘std::basic_ostream<_CharT, _Traits>’ and ‘const char [6]’
     cout<<system("grep "<<stockpile[i].datepurchased<<" stockdatabase.txt")<<endl;

当我尝试使用它运行时,它工作正常:

 cout<<system("grep '9oct16' stockdatabase.txt")

stockpile[i].datepurchased 是我可以 cout 存储在我的文本文件中的不同日期的地方,我可以在 for 循环中打印出 stockpile[i].datepurchased 值。 它返回字符串 9oct16 、 10oct16 等,但是当我尝试使用 shell 命令时它不会编译。

【问题讨论】:

  • "grep "&lt;&lt;stockpile[i].datepurchased 中,编译器无法理解&lt;&lt; 的含义。我也不能。

标签: c++ linux shell loops ubuntu


【解决方案1】:

&lt;&lt; 运算符是流运算符。虽然您可以在流中将字符串(和 c 字符串)与它们连接起来(例如 cout),但如果不实际处理流,它就无法以这种方式工作。

让我们把系统调用中的语句分开

"grep "<<stockpile[i].datepurchased<<" stockdatabase.txt"

&lt;&lt; 不应该在没有流对象“流”入的情况下以这种方式使用。

您可以执行以下操作:

std::string command = "grep "
                      + stockpile[i].datepurchased
                      + " stockdatabase.txt"
system(command.c_str());

这做了几件事。

  • 创建一个std::string来存储系统命令
  • 因为datepurchased 已经是std::string,您可以在其他c 字符串上使用+ 运算符来连接它们。
  • 系统期望const char* 作为参数。因此,为了能够将 c 字符串传递给函数,我们使用 std::stringc_str() 函数

您还可以将语句缩短为:

system( ("grep "+stockpile[i].datepurchased+" stockdatabase.txt").c_str());

因为 + 运算符会创建一个临时的std::string,所以您可以直接访问它的c_str() 函数。

【讨论】:

    【解决方案2】:

    好吧,您的编译器很清楚地告诉您出了什么问题:您正在尝试将流运算符 &lt;&lt; 与两种不匹配的类型一起使用,即const char [6](又名"grep ")和std::ostream(又名@ 987654325@)。您根本无法流入 char 数组或字符串。这就是 STL 中流的设计目的。所以一种可能的解决方案是:

    std::cout << system((std::string("grep ") + stockpile[i].datepurchased + std::string(" stockdatabase.txt")).c_str()) << std::endl;
    

    虽然没有测试它;)

    【讨论】:

      【解决方案3】:

      这是错误的:

      cout<<system("grep "<<stockpile[i].datepurchased<<" stockdatabase.txt")<<endl
      

      您首先需要一个 stringstream 对象来流式传输命令的各个部分。 或者使用这样的字符串构建命令:

      std::string command = "grep ";
      command +=stockpile[i].datepurchased;
      command +=" stockdatabase.txt";
      
      cout<<system( command.c_str() )<<endl
      

      【讨论】:

        【解决方案4】:

        &lt;&lt; 是附加到流的运算符,它不进行字符串连接。所以请使用+而不是&lt;&lt;来生成grep命令。

        http://www.cplusplus.com/reference/string/string/operator+/

        http://www.cplusplus.com/reference/string/string/operator%3C%3C/

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-05-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多