redirect the output of an executable

This is a discussion on redirect the output of an executable within the C Programming forums, part of the General Programming Boards category; Hi All, I need to redirect the output of an executable to a file instead of the standard output. I ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    14

    Unhappy redirect the output of an executable

    Hi All,

    I need to redirect the output of an executable to a file instead of the standard output. I wonder what the easiest way to achieve that. For example, I have an batch file, which prints out some information on screen when it is executed from command line. Now I want to write a C program, to put the output of the file to a log file (for both UNIX and Windows).

    I know it can be achieved by first opening the file, fetching each line of the outputs and then writing each line to the log file. But my feeling is there should be some easier way to reach the same goal.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Do you mean like this:
    Code:
    C:\>dir > logfile
    ...
    C:\>mybatch.bat > mybatch.log
    Or where you after something else?
    --
    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.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    system ( "program.exe > output.txt" ) ;

    or

    C:\> program.exe > output.txt

    or

    rename ( "old.txt" , "new.txt" ) ;

    depending on what you want.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Yes, it achieves the same goal as "C:\>mybatch.bat > mybatch.log" except for it needs to be done through programming.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    The system call should work. Thanks!

    Can I use spawn?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    In that case, I would write a program that does this:
    Code:
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
        if (argc < 2) return 1;
        else {
          FILE *f = fopen(argv[1], "w");
          char buffer[1000];
          if (f) {
             while(fgets(buffer, sizeof buffer, stdin))
                 fwrite(buffer, 1, strlen(buffer), f);
              fclose(f);
              return 0;
          }
          return 2;
    }
    It's missing error checking, but roughly does what you want.

    Use it like this: "mybatch | mylogger mybatch.log"

    --
    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.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Hi Mats,

    I know your code can achieve the goal. But is your method better than the "system" call and why?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    No, it's not better - but why do you want to do it from a C program in the first place, if the best solution (in my mind) is to redirect the batch file in the first place. If you want to avoid the user doing the redirection, you can either use a Windows shortcut, or use another batch-file to produce the desired result. This is the most efficient method, and also the most "simple" solution from a programming perspective - you just have to put the command(s) into a batch file and that's job done.

    If you can do it using a batch-file, then that's as good as calling "system()" - there is no great deal of difference other than that you have to write some C-code that does the calling of a system command - not much point in that, in my opinion.

    Perhaps you need to explain further what you are actually trying to achieve...

    --
    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.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Another suggestion: the POSIX function popen() might do what you're looking for: http://opengroup.org/onlinepubs/0079...xsh/popen.html

    popen() lets your program start another program and then take the output from that, rather than having to launch the other program externally for example with "program | program". You could, for example, launch the batch file from within the program, and grab its output.

    Note: popen() is not ANSI standard. If your system doesn't have it, I'm sure there is a Win32 equivalent, though I'm not sure what it would be. Perhaps _popen() on MSVC.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by robwhit View Post
    system ( "program.exe > output.txt" ) ;

    or

    C:\> program.exe > output.txt

    or

    rename ( "old.txt" , "new.txt" ) ;

    depending on what you want.
    Hmm this is a bit of a confusing thread as it is a bit unclear what the question is.
    Is it I want to call a batch file from a program and have the output of the batchfile redirected into a file? I guess that is what it seems to be.

    I am just wondering if you can just call
    Code:
    system("batchfile");
    And then just redirect the output of the program eg
    Code:
    program>results
    Or whether the batchfile is 'unaware' of the redirection?

    Anyway it seems it is aware of the redirection, so it may be better to do it that way
    so that the redirection is not 'hard coded' into the program?
    I guess it depends on what you want.

  11. #11
    Registered User
    Join Date
    Feb 2008
    Posts
    14
    Thanks all for the useful feedback.

    I think the following method fits my goal:

    Code:
    system ( "program.exe > output.txt" ) ;
    I don't want to add another batch file to my project, just to redirect the output. And I don't think it is necessary to write codes to manually put every output line to a file if the "system" call is as good.

    Actually the output file will be used for later parsing, which is not supposed to be used by users.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  2. redirect console output to a String?
    By TwistedMetal in forum C Programming
    Replies: 10
    Last Post: 02-26-2008, 02:54 PM
  3. Shell Scripting...redirecting executable output?
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-13-2005, 11:50 AM
  4. Trying to store system(command) Output
    By punxworm in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 07:46 PM
  5. how to redirect the output of a program?
    By dkt in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 01:05 AM

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