redirecting stdout

This is a discussion on redirecting stdout within the C Programming forums, part of the General Programming Boards category; I know of the DOS command to redirect stdout Code: C:> myprogram >info.txt using this command, or some other method ...

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    redirecting stdout

    I know of the DOS command to redirect stdout
    Code:
    C:> myprogram >info.txt
    using this command, or some other method outside of coding it into the program, is there a way that I can have stdout go to the screen and a file???

  2. #2
    Registered User
    Join Date
    Sep 2003
    Posts
    34
    Well I'm new to C so I may be a little off. I think you could just write your own function that you pass text to, then you have it print it to the screen and write it to a file. There might be an easier way but that's all I know of.
    -gunder

    if (problem)
    postcount++;

  3. #3
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    What you said would work perfectly, but please notice I said outside of coding it myself. I want to be able to do this for programs that I didn't write.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    In *nix you'd use the tee command, but I don't know of a Windows console equivalent. But it's pretty easy to write one.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char **argv)
    {
      FILE *fp;
      int  c;
      
      if (argc != 2 || (fp = fopen(argv[1], "w")) == NULL)
      {
        perror (argv[1]);
        return EXIT_FAILURE;
      }
      
      while ((c = fgetc(stdin)) != EOF)
      {
        fputc (c, fp);
        fputc (c, stdout);
      }
      
      fclose(fp);
      return(0);
    }
    
    /* 
    
    Usage:
    
    echo "blah" | thisprog.exe out.txt
    
    */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    thanks Hammer. I'm not fully clear on the usage still. Would I be able to use that program to open another program and have its output go to both screen and file?

  6. #6
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    No, you use thisprog.exe to grab the output from another program and thisprog.exe will output to the screen and the file specified.

    The syntax of the program as described by hammer is a little confusing. The syntax based on your need would be

    C:> myprogram | thisprog.exe out.txt

    The output of myprogram that goes to stdout will be piped ('|') into thisprog.exe. thisprog.exe then actually handles the output to the screen and to the file out.txt
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  7. #7
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Ah...thanks a lot! I got it now.

  8. #8
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I've hit a snag....it works...but I can't use input anymore

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Draco
    I've hit a snag....it works...but I can't use input anymore
    You need to be more specific.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I put the program into the command line like WaltP said...it opened up the program and created a file for the output, but nothing I did on the keyboard would register as input, not even CTRL+C to force it to close.

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Strange, works OK for me in a Windows Console.

    Code:
    c:>junk2 | junk1 out.txt
    blah blah blah
    ^Z
    blah blah blah
    
    c:>type out.txt
    blah blah blah
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by Draco
    I put the program into the command line like WaltP said...it opened up the program and created a file for the output, but nothing I did on the keyboard would register as input, not even CTRL+C to force it to close.
    As Hammer said, you need to be specific.

    I put the program which program? into the command line like WaltP said...it opened up the program which program? and created a file for the output, but nothing I did on the keyboard would register as input, not even CTRL+C to force it to close. what is supposed to accept keyboard input?

    It helps to tell us what the program before the pipe is supposed to do, and how that is interacting with the filter program....

    ***Lightbulb***
    The program you are trying to run accepts keyboard input -- this is possibly the problem. When you pipe information, all the input is buffered and sent to the filter program in chunks. Therefore you can't write prompts to stdio. Output everything that must go to the screen to stderr with fprintf()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    I think you've hit the nail on the head, WaltP. I've been trying it with different programs I've made before I tried it on other programs, and all of mine use stdio..........

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Ahh, the sweet smell of burning grey-matter (cough!)
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirecting stdout to socket
    By rancor in forum C Programming
    Replies: 9
    Last Post: 10-18-2008, 05:18 AM
  2. redirecting stdout
    By ichijoji in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2006, 09:20 PM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 09:42 AM
  4. redirecting stdout
    By gregulator in forum C Programming
    Replies: 2
    Last Post: 04-22-2004, 10:07 AM
  5. redirecting stdout to a socket
    By Kinasz in forum Linux Programming
    Replies: 2
    Last Post: 03-25-2004, 07:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21