[cs631apue] [CS631] doubt on dup2

Jan Schaumann jschauma at stevens.edu
Mon Nov 6 15:30:07 EST 2017


Jeevanandha Ramanathan <jramana1 at stevens.edu> wrote:
 
> I am not getting the output redirected to the file. Instead it's just
> printing on the screen.

You're duplicating your open file descriptor onto stdout.

Try the other way around:

--- dupdemo2.c.orig     2017-11-06 15:29:24.000000000 -0500
+++ dupdemo2.c  2017-11-06 15:27:37.000000000 -0500
@@ -10,7 +10,7 @@
     perror("unable to open the file");
     exit(1);
   }
-  if((r = dup2(STDOUT_FILENO, file))!=file){
+  if((r = dup2(file, STDOUT_FILENO))!=STDOUT_FILENO){
     perror("unable to dupe the stdout to file");
   }
   

-Jan


More information about the cs631apue mailing list