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

File Path generate using std::ofstream

本文关键字:文件 路径 ofstream std 使用      更新时间:2023-10-16

以下代码正常工作

    //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();
    }

但如果我将moduleVerilogFile更改为:

std::ofstream moduleVerilogFile(m_outputFilePath + "\Verilog\" + m_moduleName + ".v");

由于moduleVerilogFile.is_open()返回false,因此不会向文件中写入任何内容我应该显式生成Verilog目录吗?(看起来很奇怪)

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