c ++中bash(system-> wget)命令的问题

时间:2010-11-25 17:39:37

标签: c++ bash

这是我的代码:

string line;
ifstream toOpen;
toOpen.open("allfiles.txt", ios::in);
int fileCounter=0;

if(toOpen.is_open()){
    while(!toOpen.eof()){
        getline(toOpen,line);
        string dl = "wget -q -E -O  superman/" + href[0] + " " + line;
        //cout << dl << endl;
        fileCounter++;
        system(dl);
    }

    toOpen.close();
}

allfiles.txt(内容):

http://www.xxx.com/index1.html
http://www.xxx.com/index2.html

href []的值如下:{index1.html,index2.html,index3.html ...}

我的错误讯息:

file.cpp:XX: error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int system(const char*)’

3 个答案:

答案 0 :(得分:3)

函数'system'将'const char *'作为参数,但你给它一个std :: string。尝试

system(dl.c_str());

答案 1 :(得分:2)

system想要一个const char *参数,因此您必须致电dl.c_str()以获取char * std::string的数据。

    system(dl.c_str());

答案 2 :(得分:1)

system()对C ++类型一无所知。你必须这样给它char*

system(dl.c_str());