saving the o/p of system() in a string
the system() function executes an OS command . it returns 0 if command is executed else it returns some integer.
as an example :
Code:
#include <stdlib.h>
int main()
{
system("date");
}
this gives me the date on execution.
how do i save the string generated by date command into a character array variable(like char buf[50])?
the only thing that i could think of so far is to redirect the stream, to a file at OS level and then open that file and read from it.
like this
Code:
#include <stdlib.h>
#include<stdio.h>
int main()
{
system("date >f1");
FILE *fp;
char ch;
fp=fopen("f1","r");
while((ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
}
fclose(fp);
remove("f1");
}
this obviously is pretty lame.
i would want to redirect the output(not the return variable) to a string(char array) variable directly....I want to use C ... instead of using OS
Re: saving the o/p of system() in a string
See popen() in your manual.
gg
Re: saving the o/p of system() in a string
Is popen() available on a ported-gcc compiler on windows too?
Re: saving the o/p of system() in a string
Should be available to both Cygwin and MinGW.
gg