我想編寫一個生成gz文件的函數。該功能只能在Linux
上運行,所以我想使用gzip
命令(只需執行外部命令)。
到目前爲止,我有這樣的:C++ - 在執行之前知道Linux命令是否存在的方法
bool generate_gz(const String& path)
{
bool res = false;
// LINUX
#ifndef __WXMSW__
if(!gzip_command_exists())
cout << "cannot compress file. 'gzip' command is not available.\n";
else
res = (0 == execute_command(String::Format("gzip %s", path.c_str())));
// WINDOWS
#else
// do nothing - result will be false
#endif
return res;
}
bool gzip_command_exists()
{
// TBD
}
問題
是否有實現的方法嗎?如果是這樣,是否需要運行(或試圖運行)命令gzip
?
有一種方法,但沒有必要。無論如何,你必須檢查運行的結果。試着運行命令並報告任何失敗。 –
運行命令的退出代碼告訴我它在進程中是否存在並失敗,或根本不存在?我想區分這些失敗。 – idanshmu
試試'man execve'並查看errno描述。 (你可能應該使用'exec'系列之一而不是'system')。 –