the system() function executes an OS command . it returns 0 if command is executed else it returns some integer.
as an example :
this gives me the date on execution.Code:#include <stdlib.h> int main() { system("date"); }
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
this obviously is pretty lame.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"); }
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




Reply With Quote