C++ std::ofstream open的klocwork问题

C++ std::ofstream open的klocwork问题,c++,ofstream,klocwork,C++,Ofstream,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::

克洛茨克投掷

为“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();
}
#包括
#包括
void main()
{
std::ofs流;
open(“file.txt”,std::ofstream::out);
如果(ofs.is_open())
{

std::cout您是否仍有此问题? 我有一个可以满足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 );
        }  

我的猜测可能是“工具的误报”-很难避免,总会有一些误报..这可能是因为没有为ofs.open(“file.txt”,std::ofstream::out)的结果分配变量;