![]() |
| | #1 |
| Registered User Join Date: Dec 2005
Posts: 167
| Redirect stdout using dup Code: int main(int argc, char *argv[])
{
int f = open("test.txt", O_CREAT|O_RDWR, 0666);
int out=dup(1);
if(f==-1)
perror("open()");
dup2(f, 1);
printf("Hello world\n");
printf("%d\n",close(f));
close(1);
printf("test\n");
return 0;
}
Please help! |
| spank is offline | |
| | #2 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| You probably want to restore the original stdout after you've dup'd it to be f. -- Mats
__________________ Compilers can produce warnings - make the compiler programmers happy: Use them! Please don't PM me for help - and no, I don't do help over instant messengers. |
| matsp is offline | |
| | #3 |
| Registered User Join Date: Dec 2005
Posts: 167
| yap! |
| spank is offline | |
| | #4 | |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| Also, using dup2() is usually redundant. These two are equivalent: Code: fd = open(...); dup2(fd, 1); Code: fd = open(...); close(1); dup(fd); The first example is one line of code shorter, but the second is generally preferable, because of this: Quote:
| |
| brewbuck is offline | |
| | #5 |
| Registered User Join Date: Dec 2005
Posts: 167
| Code: #include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int pid, status;
int pid1, pid2;
switch(pid=fork())
{
case 0:
{
close(1);dup(0);
pid1 = fork();
if(pid1==0)
{
execl ("/bin/ls", "ls", "-1", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
pid2 = fork();
if(pid2==0)
{
execl ("/bin/cat", "cat", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
break;
}
default:
{
wait(&status);
}
}
return 0;
}
|
| spank is offline | |
| | #6 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| You aren't calling pipe() anywhere. Without creating a pipe, how do you expect data to get from one process to another? |
| brewbuck is offline | |
| | #7 | |
| Cat without Hat Join Date: Apr 2003
Posts: 8,579
| Quote:
Code: #include <stdio.h>
#include <unistd.h>
int main(void)
{
int sout = dup(1);
close(1);
close(2);
int sin2 = dup(0);
dup2(sout, 1);
printf("dup(stdin): %d\n", sin2);
return 0;
}
dup(stdin): 1
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law | |
| CornedBee is offline | |
| | #8 |
| Registered User Join Date: Dec 2005
Posts: 167
| Code: #include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int pid, status;
int pid1, pid2;
int filedes[2];
pipe(filedes);
switch(pid=fork())
{
case 0:
{
//close(1);dup(0);
pid1 = fork();
if(pid1==0)
{
dup2(filedes[1], 1);
// close(filedes[0]);
execl ("/bin/ls", "ls", "-lh", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
pid2 = fork();
if(pid2==0)
{
dup2(filedes[0], 0);
// close(filedes[1]);
execl ("/bin/cat", "cat", (char *)0);
exit(-1);
}
else
{
wait(&status);
}
break;
}
default:
{
wait(&status);
}
}
close(filedes[0]);
close(filedes[1]);
return 0;
}
|
| spank is offline | |
| | #9 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,579
| exec displaces the current process.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #10 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| |
| brewbuck is offline | |
| | #11 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,579
| Problem? I was just demonstrating that my definition is universally true, while yours, despite the word "always", is just a special case (the last descriptor closed is actually the lowest free one).
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #12 |
| Staff software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 6,014
| Yeah, I was being facetious. At any rate, the close/dup trick is pervasive. |
| brewbuck is offline | |
| | #13 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,579
| So it is, but luckily only for the low descriptors stdin, stdout and stderr. Anything above that and it becomes unreliable.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #14 |
| Registered User Join Date: Mar 2007
Posts: 76
| > And second why is there the need to close the other side of the pipe ? Parent doesn't need those descriptors - two of the children need it and unless you close them, the process that reads would wait forever and I think that is why cat blocks in your case. It never receives eof or something like that. |
| idelovski is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Redirect stdout | GoLuM83 | C Programming | 6 | 12-15-2006 04:17 PM |
| redirect stdout to editbox | kerm1t | Windows Programming | 1 | 05-11-2005 10:32 PM |
| Problems with switch() | duvernais28 | C Programming | 13 | 01-28-2005 10:42 AM |
| Who to redirect stdout to a file in C | edugarcia | Linux Programming | 3 | 10-01-2004 12:18 AM |
| redirect stdout to a file? | Brian | C Programming | 5 | 01-15-2002 01:06 PM |