Loading ...
Sorry, an error occurred while loading the content.

Re: [c-prog] redirect stdout/stderr to file

Expand Messages
  • RK Sahoo
    dup2(fn,1); closes 1 i.e. stdout and makes it the copy of fn. so stdout is now a copy of fn... ... __________________________________________________ Do You
    Message 1 of 20 , Jun 1, 2001
    View Source
    • 0 Attachment
      dup2(fn,1);
      closes 1 i.e. 'stdout' and makes it the copy
      of fn. so stdout is now a copy of fn...


      --- Paul McKernon <p.mckernon@...> wrote:
      > Oh sorry - got carried away :)
      >
      > I looked at the man page for dup2 and the 2nd arg is
      > the destination of
      > the fd copy. Could the writer explain the syntax
      > please. I for one am
      > confused. Maybe it sould be the other way around?
      >
      >
      > From the man page (red hat linux):
      >
      > int dup2(int oldfd, int newfd);
      > dup2 makes newfd be the copy of oldfd, closing newfd
      > first if necessary.
      >
      > -Paul
      >
      >
      >
      > "Matthews, John" wrote:
      > >
      > > steady- semi-educated guess only
      > >
      > > > -----Original Message-----
      > > > From: Paul McKernon
      > [mailto:p.mckernon@...]
      > > >
      > > > Ahhh - I was furiously headscratching there!
      > > >
      > > > "Matthews, John" wrote:
      > > > >
      > > > > Just to clarify,
      > > > >
      > > > > dup2(fn,1); /* 1 = fileno(stdout) ? */
      > > > > dup2(fn,2); /* 2 = fileno(stderr) ? */
      >
      >
      >
      > Your use of Yahoo! Groups is subject to
      > http://docs.yahoo.com/info/terms/
      >
      >


      __________________________________________________
      Do You Yahoo!?
      Get personalized email addresses from Yahoo! Mail - only $35
      a year! http://personal.mail.yahoo.com/
    • Paul McKernon
      Ok - cool. Sorry - I was just a little confused. Thanks for clearing it up. -Paul PS - how do you make stdout go back to the screen then? Do you need to store
      Message 2 of 20 , Jun 1, 2001
      View Source
      • 0 Attachment
        Ok - cool. Sorry - I was just a little confused. Thanks for clearing it
        up.

        -Paul

        PS - how do you make stdout go back to the screen then? Do you need to
        store a copy of the stdout fd before overwriting it?


        RK Sahoo wrote:
        >
        > dup2(fn,1);
        > closes 1 i.e. 'stdout' and makes it the copy
        > of fn. so stdout is now a copy of fn...
      • RK Sahoo
        check this .......... #include #include main() { FILE* fp=fopen( redir.txt , w ); int fn=fileno(fp); printf( user file n ); int x=100; ++x;
        Message 3 of 20 , Jun 1, 2001
        View Source
        • 0 Attachment
          check this ..........


          #include<stdio.h>
          #include<unistd.h>
          main()
          {
          FILE* fp=fopen("redir.txt","w");
          int fn=fileno(fp);
          printf("user file\n");
          int x=100;
          ++x;
          stdout=fdopen(fn,"w");
          printf("stdout => user file\n");
          stdout=fdopen(1,"w");
          printf("stdout => stdout\n");

          }

          hope that solves ur purpose..............

          --- Paul McKernon <p.mckernon@...> wrote:
          > Ok - cool. Sorry - I was just a little confused.
          > Thanks for clearing it
          > up.
          >
          > -Paul
          >
          > PS - how do you make stdout go back to the screen
          > then? Do you need to
          > store a copy of the stdout fd before overwriting it?
          >
          >
          > RK Sahoo wrote:
          > >
          > > dup2(fn,1);
          > > closes 1 i.e. 'stdout' and makes it the copy
          > > of fn. so stdout is now a copy of fn...
          >
          >
          >
          > Your use of Yahoo! Groups is subject to
          > http://docs.yahoo.com/info/terms/
          >
          >


          __________________________________________________
          Do You Yahoo!?
          Get personalized email addresses from Yahoo! Mail - only $35
          a year! http://personal.mail.yahoo.com/
        • Matthews, John
          Thanks RK BTW did a quick check on my Unix system, and as expected: fileno(stdin) == 0 fileno(stdout) == 1 fileno(stderr) == 2 But I don t know whether these
          Message 4 of 20 , Jun 1, 2001
          View Source
          • 0 Attachment
            Thanks RK

            BTW did a quick check on my Unix system, and as expected:

            fileno(stdin) == 0
            fileno(stdout) == 1
            fileno(stderr) == 2

            But I don't know whether these are guaranteed (?); better to use fileno(fp)
            for clarity in real code.

            John

            > -----Original Message-----
            > From: RK Sahoo [mailto:sahoo_tm@...]
            >
            > dup2(fn,1);
            > closes 1 i.e. 'stdout' and makes it the copy
            > of fn. so stdout is now a copy of fn...
          • Chad Cederberg
            i forget to mention i m looking for an ansi c solution (must be portable). where are these functions typically defined: fileno fdopen dup2 i absolutely need to
            Message 5 of 20 , Jun 1, 2001
            View Source
            • 0 Attachment
              i forget to mention i'm looking for an ansi c solution (must be portable).

              where are these functions typically defined:
              fileno
              fdopen
              dup2

              i absolutely need to have the code to be compatible with winnt bsdunix and
              linux.

              both code samples (below) use <unistd.h> which is included with linux and
              bsdunix,
              but how about winnt.

              i'm using lcc-win32 compiler. i beleive <unistd.h> is not included with
              this distribution.
              what is required to have access to these functions on winnt.

              isn't winnt suppose to be posix compliant?
              if <unistd.h> holds functions required for posix, why is not included.
              is it just my compiler distribution.

              -chad


              -----Original Message-----
              From: RK Sahoo [mailto:sahoo_tm@...]
              Sent: Friday, June 01, 2001 8:40 AM
              To: c-prog@yahoogroups.com
              Subject: Re: [c-prog] redirect stdout/stderr to file


              check this ..........


              #include<stdio.h>
              #include<unistd.h>
              main()
              {
              FILE* fp=fopen("redir.txt","w");
              int fn=fileno(fp);
              printf("user file\n");
              int x=100;
              ++x;
              stdout=fdopen(fn,"w");
              printf("stdout => user file\n");
              stdout=fdopen(1,"w");
              printf("stdout => stdout\n");

              }

              > -----Original Message-----
              > From: RK Sahoo [mailto:sahoo_tm@...]
              >
              > check following program.. this program redirects
              > stdout and stderr to "redir.txt"
              >
              >
              > #include<stdio.h>
              > #include<unistd.h>
              > main()
              > {
              > FILE* fp=fopen("redir.txt","w");
              > int fn=fileno(fp);
              > dup2(fn,1);
              > dup2(fn,2);
              > printf("stdout\n");
              > fprintf(stderr,"stderr\n");
              > fclose(fp);
              > }
            • Thomas J. Hruska
              ... Try including on Win9x/NT/DOS machines. Thomas J. Hruska -- shinelight@crosswinds.net Shining Light Productions -- Meeting the needs of fellow
              Message 6 of 20 , Jun 1, 2001
              View Source
              • 0 Attachment
                At 11:21 AM 6/1/01 -0500, you wrote:
                >i forget to mention i'm looking for an ansi c solution (must be portable).
                >
                >where are these functions typically defined:
                >fileno
                >fdopen
                >dup2

                Try including <io.h> on Win9x/NT/DOS machines.


                Thomas J. Hruska -- shinelight@...
                Shining Light Productions -- "Meeting the needs of fellow programmers"
                http://www.shininglightpro.com/
              • Thomas J. Hruska
                ... Try including on Win9x/NT/DOS machines. Thomas J. Hruska -- shinelight@crosswinds.net Shining Light Productions -- Meeting the needs of fellow
                Message 7 of 20 , Jun 1, 2001
                View Source
                • 0 Attachment
                  At 11:21 AM 6/1/01 -0500, you wrote:
                  >i forget to mention i'm looking for an ansi c solution (must be portable).
                  >
                  >where are these functions typically defined:
                  >fileno
                  >fdopen
                  >dup2

                  Try including <io.h> on Win9x/NT/DOS machines.


                  Thomas J. Hruska -- shinelight@...
                  Shining Light Productions -- "Meeting the needs of fellow programmers"
                  http://www.shininglightpro.com/
                • Thomas J. Hruska
                  ... Oops...I must be tired. I said machines . I meant _under Win9x/NT/DOS._ .-) . -)
                  Message 8 of 20 , Jun 1, 2001
                  View Source
                  • 0 Attachment
                    At 12:46 AM 6/2/01 -0500, you wrote:
                    >At 11:21 AM 6/1/01 -0500, you wrote:
                    >>i forget to mention i'm looking for an ansi c solution (must be portable).
                    >>
                    >>where are these functions typically defined:
                    >>fileno
                    >>fdopen
                    >>dup2
                    >
                    >Try including <io.h> on Win9x/NT/DOS machines.

                    Oops...I must be tired. I said 'machines'. I meant _under Win9x/NT/DOS._

                    <rolls eyeballs>
                    '.-)
                    .'-) <--- 3 Frames of "Animation"
                    :-)


                    Thomas J. Hruska -- shinelight@...
                    Shining Light Productions -- "Meeting the needs of fellow programmers"
                    http://www.shininglightpro.com/
                  • thompson mindi
                    plzzzz i dont want any of your mails i want to unscribe this programme plzzz so dont send any mails __________________________________________________ Do You
                    Message 9 of 20 , Jun 2, 2001
                    View Source
                    • 0 Attachment
                      plzzzz i dont want any of your mails i want to
                      unscribe this programme
                      plzzz so dont send any mails

                      __________________________________________________
                      Do You Yahoo!?
                      Get personalized email addresses from Yahoo! Mail - only $35
                      a year! http://personal.mail.yahoo.com/
                    • Ranju
                      ... Kewl :). -- Ranju. V http://www.geocities.com/cool_ranju/ --
                      Message 10 of 20 , Jun 2, 2001
                      View Source
                      • 0 Attachment
                        | <rolls eyeballs>
                        | '.-)
                        | .'-) <--- 3 Frames of "Animation"
                        | :-)

                        Kewl :).

                        --
                        Ranju. V
                        http://www.geocities.com/cool_ranju/
                        --
                      Your message has been successfully submitted and would be delivered to recipients shortly.