Zlib调用gzopen和gzclose后返回错误代码2

时间:2020-09-25 09:46:40

标签: c++ zlib

我有一个看起来类似于下面代码的compress函数。它基本上从我的应用程序中获取日志文件,并使用Zlib对其进行压缩。一个用户报告这样的错误:Failed compressing file: 'C:/ProgramData/AppName/logs/cef.log'; error number 2.我真的很讨厌C,它是错误代码。有人可以指出问题的原因吗?

string contents;

if ( ! fileContents(source, contents))
    throw FailedCompressionError{"Failed to get the file contents of '" + source + "'."};

gzFile targetFile{gzopen(target.c_str(), "wb")};

const auto result{gzclose(targetFile)};
if (result != Z_OK)
    Logger::Error(
        "Failed closing file: '{}'; error number {}; result {}.",
        target,
        errno,
        result
    );
    
if (targetFile == Z_NULL)
    throw FailedCompressionError{
        "Failed compressing file: '" + source + "'; error number " + to_string(errno) + '.'
    };

const auto contentsLength{static_cast<unsigned>(strlen(contents.c_str()))};
auto result{gzwrite(targetFile, contents.c_str(), contentsLength)};

(...)

1 个答案:

答案 0 :(得分:1)

那不是zlib错误代码。这是一个stdio errno代码。 2表示找不到文件。

相关问题