Gilson PJ Gilson PJ - 9 months ago 71
Linux Question

Why file is not created in /etc/ directory

Please find the code sample

void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);


File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.

Answer Source

There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.

In order to be able to write to /etc, your program must be executed as root.