C++ 使用std::ofstream生成文件路径

C++ 使用std::ofstream生成文件路径,c++,file,ofstream,C++,File,Ofstream,下面的代码工作正常 //m_outputFilePath and m_moduleName are strings std::string moduleVerilog; // call Gen function to update moduleVerilog std::ofstream moduleVerilogFile(m_outputFilePath + "\\" + m_moduleName + ".v"); if (moduleVerilog

下面的代码工作正常

    //m_outputFilePath and m_moduleName are strings

     std::string moduleVerilog;
    // call Gen function to update moduleVerilog

    std::ofstream moduleVerilogFile(m_outputFilePath + "\\" + m_moduleName + ".v");
    if (moduleVerilogFile.is_open())
    {
        moduleVerilogFile << moduleVerilog;
        moduleVerilogFile.close();
    }
由于
modulerilogfile.is\u open()
返回false,因此不会向文件写入任何内容
我应该显式生成
Verilog
目录吗?(看起来很奇怪)

如果
Verilog
目录不存在,
std::ofstream
不会为您创建它-它的目的是读取和写入文件。如果目录不存在,它将无法打开文件,因此
is\u open
将确实为false。

文件可能不存在,但目录路径必须有效。如果文件不在有效路径中,默认情况下,
std::ofstream
将创建一个文件并将内容写入其中。
std::ofstream moduleVerilogFile(m_outputFilePath + "\\Verilog\\" + m_moduleName + ".v");