【发布时间】:2014-01-23 09:45:04
【问题描述】:
我想写一个生成 gz 文件的函数。该功能只能在Linux 上运行,所以我想使用gzip 命令(只需执行外部命令)。
到目前为止,我有这个:
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_command_exists()?如果是这样,它是否必须涉及运行(或尝试运行)gzip 命令?
【问题讨论】:
-
有办法,但没必要。无论如何,您必须检查运行的结果。只需尝试运行命令并报告任何失败。
-
运行命令的退出代码是否告诉我它是否存在并且在进程中失败,或者根本不存在?我想区分这些失败。
-
尝试
man execve并查看错误描述。 (您可能应该使用exec系列之一,而不是system)。 -
随意告诉我在这个问题中究竟有什么不清楚或没有用的,值得一票否决。这样我就可以改进它。
-
我对这个问题投了反对票,因为稍微研究一下就会告诉你
zlib和gzopen或者只是system(3) 可能会失败。