简体   繁体   English

在C ++ Ubuntu Linux中运行Shell命令时出错

[英]Error when running shell command in C++ ubuntu linux

//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; 
}

When I try to compile it gives me this error : 当我尝试编译时,出现此错误:

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;

When I try to run with this it works fine : 当我尝试使用它运行正常时:

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

stockpile[i].datepurchased is where I can cout the different dates stored in my textfile , I can print out stockpile[i].datepurchased values in the for loop. stockpile[i].datepurchased是在那里我可以cout存放在我的文本文件在不同的日期,我可以打印出stockpile[i].datepurchased值在for循环。 It returns string 9oct16 , 10oct16 etc. but when I try to use shell command it wont compile . 它返回字符串9oct16,10oct16等,但是当我尝试使用shell命令时,它将不会编译。

The << operator is a stream operator. <<运算符是流运算符。 While you can concatenate strings (and c-strings) with them on a stream (like cout ) it does not work that way without actually working on a stream. 尽管您可以在流(例如cout )上将字符串(和c字符串)连接在一起,但如果不实际在流上工作,它就无法正常工作。

Lets just take the statement inside your system call separately 让我们将语句分别放在系统调用中

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

The << is not meant to be used that way without a stream object to "stream" into. 在没有流对象“流”入的情况下, <<不能用于这种方式。

What you can do though is the following: 但是,您可以执行以下操作:

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

This does several things. 这做了几件事。

  • create a std::string to store the system command 创建一个std::string来存储系统命令
  • because datepurchased is a std::string already you can use the + operator on the other c-strings to concatenate them. 因为datepurchased已经是std::string了,所以您可以在其他C字符串上使用+运算符将它们连接起来。
  • system is expecting a const char* as argument. 系统期望使用const char*作为参数。 So to be able to pass the c-string to the function we use the c_str() function of the std::string 因此,为了将c字符串传递给函数,我们使用std::stringc_str()函数

You can also shorten the statement to this: 您也可以将语句缩短为:

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

Because a temporary std::string will be created by the + operator you can access its c_str() function directly. 因为一个临时的std::string将由+运算符创建,所以您可以直接访问其c_str()函数。

<< is an operator to append to a stream, it does not do string concatenation. <<是要附加到流的运算符,它不执行字符串连接。 So please use + instead of << to generate the grep command. 因此,请使用+而不是<<来生成grep命令。

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

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

This is wrong: 这是错误的:

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

Your need a stringstream object to stream the parts of the command first. 您需要一个stringstream对象来首先流传输命令的各个部分。 Or build the command using string like that: 或使用如下字符串构建命令:

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

cout<<system( command.c_str() )<<endl

Well, your compiler quite clearly tells you what's wrong: You are trying to use the stream operator << with two non-matching types, namely const char [6] (aka "grep " ) and a std::ostream (aka stockpile[i].datepurchased ). 好吧,您的编译器很清楚地告诉您出了什么问题:您正在尝试将流运算符<<与两个不匹配的类型配合使用,即const char [6] (又称"grep " )和std::ostream (又称stockpile[i].datepurchased )。 You simply cannot stream into char arrays or strings. 您根本无法流式传输到char数组或字符串。 That's what streams are designed for in STL. 这就是STL中设计的流。 So one possible solution could be: 因此,一种可能的解决方案可能是:

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

Didn't test it, though ;) 不过没有测试;)

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

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