exec output
-
Hi all! I want to call execlp("ls","ls","-tl",(char*)0); but I also want to redirect the output to a file or a buffer or something. does anyone know of a way to get the output from exec? by the way I tried this way of piping execlp("ls","ls","-tl",">",filename,(char*)0); but this does not work. Thanks in Advance:((:confused:
-
Hi all! I want to call execlp("ls","ls","-tl",(char*)0); but I also want to redirect the output to a file or a buffer or something. does anyone know of a way to get the output from exec? by the way I tried this way of piping execlp("ls","ls","-tl",">",filename,(char*)0); but this does not work. Thanks in Advance:((:confused:
Seeing as 'ls' only exists on Unix, then I will assume that is the platform you are programming for. To control the standard output/input you need to do change the target for them in the child process (you should be using fork if you are using execl). This can be achieved with the 'dup2' command:
int myfd = open ("somefile.txt", O_CREAT | O_WRONLY, 0777); dup2 (myfd, STDOUT_FILENO);
This re-maps the stdout output to your own file. You can then call 'execlp' to load ls, and the output will go to your file