为什么不在/ etc /目录中创建文件

时间:2016-10-30 16:13:06

标签: c++ linux c++11

请找到代码示例

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

文件未在/ etc /目录中创建。我认为这与权限有关。我需要在代码中添加多少工作。

2 个答案:

答案 0 :(得分:4)

没有什么额外的东西可以添加到此程序中以使其工作&#34;。如果任意程序可以写入/etc,这将把传统的POSIX安全模型抛到窗外。

为了能够写入/etc,您的程序必须以root身份执行。

答案 1 :(得分:1)

这似乎是一个许可问题。尝试使用sudo运行程序:

sudo yourprogram
相关问题