在 Redhat 中从命令行使用 unzip 时检测覆盖
可能这应该在超级用户上,但我在代码中使用它,所以我想从这里开始。
我的 C++ 程序使用“system()”调用解压缩 .gz 文件。 (是的,我知道这是禁忌,但当我不久前开始这个项目时,这是最好的,我问在 Redhat 上从 C++ 解压文件:system() 的替代方案 当我认为我有机会改进它时,但是我还没有设法实现任何东西)
由于有可能已经存在同名文件,因此解压缩调用使用“-o”修饰符进行覆盖。
有没有办法检测是否存在覆盖?我对 system() 和 unzip 的替代方案持开放态度。
操作系统:Redhat
语言:C++
IDE:Eclipse
Possibly this should be on Superuser, but I'm using it in code so thought I'd start here.
My C++ program unzips a .gz file using the "system()" call. (Yes I know it's taboo, but it was the best of a bad lot when I started this project a while back, and I asked Unzipping a file from C++ on Redhat: alternatives to system() when I thought I'd have the chance to improve it, but I haven't managed to implement anything yet)
As there is a chance that a file with the same name already exists, the unzip call uses the "-o" modifier to for an over-write.
Is there a way to detect that there has been an overwrite? I am open to alternatives to system() and unzip.
OS: Redhat
Language: C++
IDE: Eclipse
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信显而易见的答案是在运行
system
命令之前尝试检测磁盘上是否存在具有适当名称的文件;如果解压前文件已存在,则会发生覆盖。您可以通过使用"r"
模式fopen
轻松检查文件是否存在,并检查有效文件句柄的返回值。 (如果文件已经存在,请不要忘记fclose
该文件。)I believe the obvious answer would be to try to detect whether there is a file with the appropriate name on disk before you even run the
system
command; if a file exists prior to unzipping, then an overwrite will occur. You can easily check whether a file exists byfopen
ing it with a mode of"r"
and checking the return value for a valid file handle. (Don't forget tofclose
the file if it does already exist.)