I've done a lot of research on how to redirect stdout to a variable in C but no success. All I see is redirection of stdout to a file using dup2 and freopen which is not what I need. I want to know if is it even possible to redirect it to a variable without redirecting it first to a file?
Im using ubuntu and C to create the code. Im uaing GCC 4.7.3 as the compiler.
I did use dup2 and freopen and tried to do some work around but after some time. I realized that redirecting it to a file first then to variable is a bad idea (because of the data that will be written to it). I used freopen, but it's not working, maybe because gcc 4.7.3 doesn't have the library that contains that.
Is it possible to redirect stdout to a variable? if yes, how? if you dont know exactly how. Kindly give me an idea and I'll be the one to research on that.
Edit the post to add my code
As i've mentioned above, I did use dup2 and redirect stdout to a file. The code below shows how I managed to do it. It;s working but I realized saving the data to a file should not be an option. If its possible to change just a portion of my code, what can I do to to be able to redirect the stdout to a variable?
Here's the code..
char com1[] = "sudo mmm\n";
if (execCom!=NULL)
{
fp=popen(com1,"w");
com = strtok(execCom, "\n");
// walk through other commands that is separated by \n
while( com != NULL )
{
strcpy(comR,"\0");
strcat(comR,com);
strcat(comR,"\n");
fwrite(comR, 1, strlen(comR),fp);
com = strtok(NULL, "\n");
}
pclose(fp);
fclose(out);
}