Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

system() output to a char*

Tags:

c

file

system

int system(const char *)

How can I send output of this command (lets say the command is "pwd") to a char*? Its returning an int but I want the results of the command to be sent to a char*.

like image 262
hari Avatar asked Nov 28 '25 09:11

hari


2 Answers

You can pipe the output of the command directly to a file by using "pwd > tempfile" as command.
Another way is to use popen

FILE *output = popen("pwd", "r");

That will give you a file pointer where you can read the output from.

like image 192
fab Avatar answered Nov 29 '25 22:11

fab


system("pwd > file");

system() uses "/bin/sh -c" under Linux so it can do anything a shell command line can do.

like image 32
Richard Pennington Avatar answered Nov 29 '25 23:11

Richard Pennington