How do I make a program in C generate an executable binary? [closed]

0

Hello, I wanted to know how I can for a program in C to generate another executable binary eg: binary elf x-elf.bin when executed creates another executable binary y-elf.bin

    
asked by anonymous 23.08.2017 / 05:44

1 answer

0

You can use the standard functions C: fopen , fwrite , fread and fclose using the wb flags to write or rb to read binary data.

  

The mode string can also include the letter 'b' either as a last   character or as a character among the characters in any of the   two-character strings described above. This is strictly for   compatibility with C89 and has no effect; the 'b' is ignored on all   POSIX conforming sys-tems, including Linux. (Other systems may   treat text files and binary files differently, and adding the 'b' may   be a good idea if you do I / O to a binary file and expect that your   program may be ported to non-UNIX environments.)

     

Source: man 3 fopen

If you are doing a Linux-only program simply use a fopen with the w flag and write the binary data inside the file.

Example:

#include <stdio.h>

int main()
{
        /* Criando arquivo binario */

        int dados_binarios = 1;

        FILE *file = fopen ("arquivo.bin", "wb");

        if (file != NULL)
        {
            fwrite (&dados_binarios, sizeof (dados_binarios), 1, file);
            fclose (file);
        }

    return 0;
}

More information type man 3 fopen in your terminal.

    
23.08.2017 / 09:47