【问题标题】:How to save a file to desired location in c++? [duplicate]如何将文件保存到 C++ 中的所需位置? [复制]
【发布时间】:2015-08-22 16:08:44
【问题描述】:

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

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”文件保存到同一位置的正确方法。

【问题讨论】:

  • 什么是“sys_util/environment.h”?哪个操作系统?

标签: c++ filesystems


【解决方案1】:

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

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

【讨论】:

  • 我只有“D:/project/learning/target/”,我想在目标之后添加一个文件夹“test_output”,那么我将如何实现它
  • @anamikaemail 如果您想以编程方式创建目录,可以使用CreateDirectory WIN32 函数?
  • @anamikaemail 你也可以在windows上使用_mkdir()函数。
  • 我已经创建了目录 "test_output" ,现在我将如何添加文件 "my_file.xml" 那么我将如何做呢?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2013-05-20
  • 1970-01-01
  • 2020-01-05
相关资源
最近更新 更多