How to construct a string for a Unix system() call from C program -
i'm trying create c programme run command in unix using system()
.
can create system("stat myfile")
, myfile
variable in program? there other way possible?
system()
takes pointer-to-char or char array. can build follows:
#include <stdio.h> #include <stdlib.h> char command[256]; /* big plenty "stat " + longest file name. */ char *myfile = "/my/file"; /* ... */ sprintf (command, "stat %s", myfile); system(commmand);
this should going, note hard limit 256 not bullet-proof coding due possible buffer overrun if myfile
looooong name. supercalifragilisticexpialidocious file names not unheard of :-) if want pro, next step read snprintf
manual , inquire shell getconf _posix_path_max
.
c unix command
No comments:
Post a Comment