Chris
Chris

Reputation: 362

How do I retrieve the contents of POPEN in C?

I have code that looks like this:

        char* buff = malloc(strlen(argv[1])+200);

        if (!buff)
        {
            printf("%s\n", "Error allocating memory!");
            return -1;
        }

        sprintf(buff, "%s%s%s%s", "gcc ", argv[1], " -o ", findChars(argv[1], '.'));

        FILE* command = popen(buff, "r");

        // NEED TO RETRIEVE OUTPUT OF POPEN

        pclose(command);

And this works, but I need to save the output of the POPEN to a char* so I can read it. (This is in C)

Upvotes: 0

Views: 835

Answers (1)

Thomas
Thomas

Reputation: 181745

An open pipe is just a FILE*, so any function that can read from a FILE* can do this. For example:

fgets(some_buffer, sizeof(some_buffer), command);

Error handling, repeated reads in case the buffer is too small, all the usual stuff about reading files in C also applies here.

Upvotes: 2

Related Questions