|
-
March 6th, 2009, 12:10 PM
#1
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
Last edited by creeping death; March 6th, 2009 at 12:19 PM.
-
March 6th, 2009, 01:53 PM
#2
Re: saving the o/p of system() in a string
See popen() in your manual.
gg
-
March 6th, 2009, 02:13 PM
#3
Re: saving the o/p of system() in a string
Is popen() available on a ported-gcc compiler on windows too?
-
March 6th, 2009, 02:27 PM
#4
Re: saving the o/p of system() in a string
Should be available to both Cygwin and MinGW.
gg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|