我想制作一些C++程序,我在这里使用函数popen
将命令发送到Unix中的命令行。它工作正常,但是当我拨打cd directory
时,目录不会更改。当我尝试在某些脚本中运行cd directory
时,在完成脚本目录路径更改之后,这是相同的。所以,我必须运行脚本. ./script.sh
而不是./sript.sh
,但如何用popen
函数来运行?我曾尝试在popen
的第一个参数前添加". "
,但运行". ls"
会发生错误。留在popen目录中
代码:
cout << "@ Command from " << session->target().full() << ": " << message.body() << endl;
//cout << "Prisla zprava" << endl;
//m_session->send("Hello World", "No Subject");
//system(message.body().c_str());
//if (message.body() == "")
FILE* outp;
char buffer[100];
string outps = "";
outp = popen(message.body().c_str(), "r");
while (!feof(outp))
{
fgets(buffer, 100, outp);
outps = outps + buffer;
}
pclose(outp);
cout << "& Output from command: " << outps << endl;
m_session->send(outps.c_str(), "Output");
在message.body();
是string
我要运行(我是从XMPP
收到此)。当string
例如为"ls"
时,它返回string
以及实际目录中的文件列表。但是当消息是"cd directory"
时,没有任何反应,例如尝试在脚本中更改目录。
你怎么chandging目录? (代码示例可能有用) – Mark 2009-10-28 10:20:11
如何更改程序中的目录? – 2009-10-28 10:20:16
有我使用它的示例代码。在这里,我尝试添加“。“就像运行脚本时一样,但是”。 ls“出错,没有”。 “目录不会改变 – 2009-10-28 10:23:59