I am trying to write a program that writes a file with some text in it, then makes that file executable. This is what I have:
#include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { char name[] = "foo"; FILE * fp; fp = fopen(name, "w"); fprintf(fp, "This file should be executable.\n"); execl("/usr/bin/chmod", "/usr/bin/chmod", "+x", name, NULL); return 0; } The problem I am having that running execl seems to remove the contents of the file I wrote. If I remove the execl, it works as expected, and writes a file with the desired text. But when I leave in the execl, it writes a file, makes it executable, but the file is blank. How do I make it so the file still has the text in it, and is executable?
fclosebeforeexeclexeclto change the Linux permission bits.opento create the file and thenfdopento get a handle.