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

Why file is not created in /etc/ directory

本文关键字:创建 文件 etc 为什么不      更新时间:2023-10-16

请查看代码示例

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/目录创建。我认为这与权限有关。我必须在代码中添加什么额外的工作

没有什么额外的东西可以添加到这个程序"使它工作"。如果一个任意程序可以写入/etc,这将把传统的POSIX安全模型扔出窗外。

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

这似乎是一个权限问题。尝试使用sudo:

运行程序
sudo yourprogram