Fixing file permissions after modifying in C++?

RPFeltz picture RPFeltz · Feb 15, 2012 · Viewed 24.1k times · Source

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.

Answer

Barham picture Barham · Oct 2, 2012

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

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

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

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