Write a file in a specific path in C++

后端 未结 7 748
無奈伤痛
無奈伤痛 2020-11-30 10:29

I have this code that writes successfully a file:

    ofstream outfile (path);
    outfile.write(buffer,size);
    outfile.flush();
    outfile.close();


        
相关标签:
7条回答
  • 2020-11-30 11:09

    It's not too clear what you're asking; if I understand correctly, you're given a filename, and you want to create the file in a specific directory. If that's the case, all that's necessary is to specify the complet path to the constructor of ofstream. You can use string concatenation to build up this path, but I'd strongly recommend boost::filesystem::path. It has all of the functions to do this portably, and a lot more; otherwise, you'll not be portable (without a lot of effort), and even simple operations on the filename will require considerable thought.

    0 讨论(0)
提交回复
热议问题