The code below is simply doing shell comand "ls" (as example).
Question is, how can I redirect output to go to the some file instead to stdout?
I tried to open file using "I/O low level open" (for writting) and to use its file descriptor to make redirection
like this:
Didn't work.
Thanks....
Question is, how can I redirect output to go to the some file instead to stdout?
Code:
int filedesc(){
int n, fd[2];
pipe (fd);
switch (fork()) {
case -1:
//error;
case 0:
close(fd[0]);
dup2(fd[1], STDOUT_FILENO);
close(fd[1]);
execlp("ls", "ls", NULL);
}
}
I tried to open file using "I/O low level open" (for writting) and to use its file descriptor to make redirection
like this:
Code:
int filedesc(){
int n, fd[2];
pipe (fd);
switch (fork()) {
case -1:
//error;
case 0:
close(fd[0]);
dup2(fd[1], fdout); //my file descriptor
close(fd[1]);
execlp("ls", "ls", NULL);
}
}
Didn't work.
Thanks....