0

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.

anamika email
  • 327
  • 9
  • 21

1 Answers1

1

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I have only "D:/project/learning/target/" and i want to add a folder "test_output" after target so how will i acheive it – anamika email Jun 08 '15 at 09:17
  • @anamikaemail If you want to create the directory programatically then maybe use the [`CreateDirectory`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa363855%28v=vs.85%29.aspx) WIN32 function? – Some programmer dude Jun 08 '15 at 09:19
  • @anamikaemail You can also use the `_mkdir()` function on windows. – Noel Jun 08 '15 at 09:26
  • I have created directory "test_output" , now how will i add file "my_file.xml" so how will i do that? – anamika email Jun 08 '15 at 09:45