Search code examples
ccompilationexecsystem

How to run a C file from within another C file?


EDIT: The question is flagged for similarity, but it is different as the file is being run in windows and "system()" is giving errors, unlike in the other question asked.

So, What I am trying to achieve is to compile and run a C program from within my C program.

  • Why? I have used used fopen to create a file output.c and using fprintf, entered a simple code snippet into it. For instance a simple code snippet looks like this.
#include <stdio.h>

int main()
{
    int a,b;
    a=10;
    scanf("%d", &b);
    for(int i=0; i< 10 ; i=i+1)
    {
        b++;
    }
    printf("%d", b);

    return 0;

}

This is an output file that is being generated, Now i want to run this code from within my original C code. Is it possible? It Yes, How?

I have tried using

system("gcc output.c -o output.out");
system("./output.out");

but it's giving me an error -->

undefined reference to `WinMain@16' collect2.exe: error: ld returned 1 exit status

For more context, What I am trying to do is open a file, convert it into C and find it's value, So, My approach so far has been to copy paste the content of the file into another file which I have created using:

followed by series of fprint.

Now, I am trying to execute that C file created! I am open to alternative method too! Thank You

Edit: This is the initial file

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *output;
     output = fopen("experi.c", "w+");
    fprintf(output, "#include <stdio.h>\n \n \nint main(){\n");
    fprintf(output, "int a,b;\n");
    fprintf(output, "a=10;\n");

            fprintf(output, "scanf(");
            fprintf(output, "\"%%");
            fprintf(output, "d\"");
            fprintf(output, ", &");
            fprintf(output, "b");
            fprintf(output, ");\n");
    
    fprintf(output, "for(int i=0; i< 10 ; i=i+1)\n");
    fprintf(output, "{\n");
    fprintf(output, "b++;\n");
    fprintf(output, "}\n");

            fprintf(output, "printf(");
            fprintf(output, "\"%%");
            fprintf(output, "d\"");
            fprintf(output, ", ");
            fprintf(output, "b");
            fprintf(output, ");\n");


    fprintf(output, "return 0;\n");
    fprintf(output, "}\n");
    
    
    system("gcc experi.c -o a.out");

    return 0;
}

Solution

  • Output written to C's stdio streams (such as output in your code) is, by design, buffered. At any given time some/all of the output may be sitting in an internal stream buffer, not yet written out to the file on disk.

    But it's the file on disk that the C compiler is going to see when you invoke

    system("gcc experi.c -o a.out");
    

    So, before calling system, you need to call fclose to indicate that you're done writing the file. Among other things, this will cause the stdio package to properly flush all buffered output to the on-disk file:

    fclose(output);
    

    You can also force the output buffer to be written to disk by calling fflush, and this leaves the file stream open so that you can write more to it later, which is sometimes useful, though probably not what you want here.