Reputation: 1587
The following code fails to create the file on /tmp. This happens even if I execute the binary as sudo
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ofstream of("/tmp/DIR1/DIR2/test_file",std::ios::out | std::ios::trunc);
of.close();
return 0;
}
If I remove DIR1/DIR2/ from the file path, a test_file does get created on /tmp. I am using linux mint.
Whats happening here?
Upvotes: 0
Views: 126
Reputation: 22152
The C++ file streams do not create directories, only files.
Before C++17 there is no standard function to create directories and you will have to use a OS specific function or an additional library, such as mkdir
on POSIX systems or CreateDirectory
on Windows, or boost::filesystem::create_directory
and others from the Boost library.
Since C++17 there is a filesystem library in the standard. Specifically you would want to took at e.g. std::filesystem::create_directory
.
Upvotes: 1