Linux 在 C++ 中修改后修复文件权限?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9287048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:34:58  来源:igfitidea点击:

Fixing file permissions after modifying in C++?

c++linuxexecutablefile-permissionsself-modifying

提问by RPFeltz

I'm saving my data in the executable file of the program. I copy it to a temporary file, overwrite a part starting at a 'magic string'and rename it to the original. I know this is a bad idea, but I'm doing it just for experimenting.

我将数据保存在程序的可执行文件中。我将它复制到一个临时文件,覆盖从“魔术字符串”开始的部分并将其重命名为原始文件。我知道这是一个坏主意,但我这样做只是为了试验。

I got everything to work so far, except for that I have to re-enable "Allow running as an executable" each time the file is replaced. What ways are there to solve this?

到目前为止,我一切正常,除了每次替换文件时我都必须重新启用“允许作为可执行文件运行”。有什么方法可以解决这个问题?

Additional information: I use linux.

附加信息:我使用 linux。

采纳答案by Barham

If you want to avoid using system(), you can use

如果你想避免使用 system(),你可以使用

#include <sys/stat.h>
int chmod(const char *path, mode_t mode);

It is documented in http://linux.die.net/man/3/chmod.

它记录在http://linux.die.net/man/3/chmod 中

See also: C++ - How to set file permissions (cross platform).

另请参阅:C++ - 如何设置文件权限(跨平台)

回答by Behnam Safari

If you include stdlib.h, you can use system("command").

如果包含stdlib.h,则可以使用system("command").

Try it:

尝试一下:

system("chmod 755 yourExeFile")