c++ - 如何将文件保存到 C++ 中的所需位置?

标签 c++ filesystems

<分区>

我正在尝试将文件保存到如下位置:

int main( int ac, char **av )
{
    TiXmlDocument tinyxml_document_object;   
    std::string file_path = av[0];
    std::replace(file_path.begin(), file_path.end(), '\\', '/');
    file_path = file_path.substr(0, file_path.find_last_of("/"));   
    file_path = file_path.substr(0, file_path.find_last_of("/") + 1); 
    file_path = file_path.substr(0, file_path.find_last_of("/") - 8);
    Environment_C::PutEnv("DATA_ROOT", file_path + "test_output/my_file.xml");
    std::string system_settings_dir = file_path + "test_output/my_file.xml";
    std::cout<< system_settings_dir.c_str();
    tinyxml_document_object.SaveFile( system_settings_dir.c_str() );
    return 0;
}

但是当我执行它时,它不会创建任何文件夹作为“test_output”。这里的文件路径是:

file_path = "D:/project/learning/target/"

我正在尝试创建另一个名为“test_output”的文件夹,其中包含“my_file.xml”。这里的 system_settings_dir 是:

system_settings_dir = D:/project/learning/target/test_output/my_file.xml

所以,请帮我看看为什么这个文件夹没有被创建?如果这不正确,请建议我将“my_file.xml”文件保存到同一位置的正确方法。

最佳答案

打开文件进行写入只会创建文件,而不是完整的目录层次结构。

在运行程序之前,您需要确保实际的目录层次结构(即 D:/project/learning/target/test_output)存在。

关于c++ - 如何将文件保存到 C++ 中的所需位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30704964/

相关文章:

linux - 如何在 Python 中将用户输入转换为文件路径

c++ - 使用 QIODevice::write 方法添加 "&#xd; "而不是回车

tags - 在 PC 和 Mac 上为图像使用标签或元数据

file - 为什么程序要/偏爱使用稀疏文件?

c++ - Node.js 插件的 node-gyp 重建的 g++ 构建错误

windows - 放置应用程序数据的最佳位置?

linux - 在 Linux 中查找已删除的打开文件的大小?

c++ - 如何在 vc++ 中以编程方式检查互联网速度/带宽

c++ - 检查函数指针类型的调用约定

c++ - 静态强制转换中的表达式