How to store an output from a UNIX command into a char with in C programming -
let's here code:
int main() { char *number; system("grep total myfile > filename"); printf(number); }
this code finds line myfile containing "total" , outputs new file called filename. trying have output set char "number" directly, instead of having write/read filename. there way this?
thanks help!
if understand right title, want output
command
executed,
#include <stdio.h> #include <stdlib.h> int main() { char number[100]; file *f = popen("echo 200", "r"); while (fgets(number, 100, f) != null) { printf( "%s\n", number ); } pclose(f); return 0; }
or want pass variable command , output variable
#include <stdio.h> #include <stdlib.h> int main() { int n = 100; char buf[100]; sprintf(buf, "echo %d > filename", n); // format command system(buf); // execute printf("%d\n", n); // print variable return 0; }
Comments
Post a Comment