Klocwork抛出
获得给‘ofs.open’的资源(“file.txt”,std::ofstream::out)可能会在这里丢失
下面的代码。
#include <iostream>
#include <fstream>
void main()
{
std::ofstream ofs;
ofs.open("file.txt", std::ofstream::out);
if (ofs.is_open())
{
std::cout << "file open success\n";
}
ofs.close();
}我没有发现任何问题与上述代码。有人能解释一下这里需要做些什么来解决这个问题吗?
发布于 2018-10-02 20:05:22
你还有这个问题吗?我有一个满足Klocwork的解决方案:使用RAII:
std::ofstream ofs( "file.txt", std::ios::binary );如果这不起作用,就用临时工。
std::ofstream temp( "file.txt", std::ios::binary );
if( !temp.is_open() )
{
temp.close();
}
else
{
m_outStream = std::move( temp );
} https://stackoverflow.com/questions/49927787
复制相似问题