How to save a file to desired location in c++?

330 views Asked by At

I am trying to save a file to a location as follows:

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;
}

But when i execute it does not create any folder as "test_output". Here the file path is:

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

And i am trying to make another folder named "test_output" containing "my_file.xml". Here system_settings_dir is:

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

So, please help me out why this folder is not getting created ? If this is not right then suggest me right way of saving "my_file.xml" file to the same location.

1

There are 1 answers

4
Some programmer dude On

Opening a file for writing creates only the file, not the full directory hierarchy.

You need to make sure that the actual directory hierarchy (i.e. D:/project/learning/target/test_output) exist before running your program.