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

C++ 为什么不在/etc/目录中创建文件,c++,linux,c++11,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 = "";

请查找代码示例

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);
void createFile(const std::string和FileName,const std::string和Content)
{
(FileName.c_str())的流;

当然,没有什么额外的东西可以添加到此程序来“使其工作”。如果任意程序可以写入
/etc
,这将把传统的POSIX安全模型扔出窗外


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

没有任何额外的东西可以添加到此程序以“使其工作”。如果任意程序可以写入
/etc
,这会将传统的POSIX安全模型抛到窗外


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

这似乎是权限问题。请尝试使用sudo运行您的程序:

sudo yourprogram

这似乎是权限问题。请尝试使用sudo运行您的程序:

sudo yourprogram
我认为这与权限有关。=>执行程序时是否有错误?如果有,是什么?如果没有…您可能想了解如何处理流错误。我认为这与权限有关。=>执行程序时是否有错误?如果有,是什么?如果没有…您可能想继续阅读如何处理流错误。