6

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.

jww
  • 97,681
  • 90
  • 411
  • 885
RPFeltz
  • 1,049
  • 2
  • 12
  • 21
  • you can add it to the make file or a script depending on what kind of scripting you know , to chmod it how you see fit when needed – pyCthon Feb 15 '12 at 02:22
  • 1
    Just so I get this straight: you're copying an executable image elsewhere, looking for a certain pattern, modifying the binary image in place, and then plan on executing it again? This can ONLY end in tears. – Chris Eberle Feb 15 '12 at 02:23
  • @Chris I know; it's much better to just store the data somewhere else. Imagine what would happen if the PC crashed while modifying the file. – RPFeltz Feb 15 '12 at 02:27
  • do you want to run "chmod +x " from inside your C++ program ? – Nir Alfasi Feb 15 '12 at 02:35
  • Or worse, imagine what might happen if it didn't. – Beta Feb 15 '12 at 02:35
  • possible duplicate of [C++ - How to set file permissions (cross platform)](http://stackoverflow.com/questions/592448/c-how-to-set-file-permissions-cross-platform) – Donal Fellows Nov 17 '12 at 21:03

2 Answers2

26

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

Community
  • 1
  • 1
Barham
  • 341
  • 4
  • 6
7

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

Try it:

system("chmod 755 yourExeFile")
RPFeltz
  • 1,049
  • 2
  • 12
  • 21
Behnam Safari
  • 2,941
  • 6
  • 27
  • 42