How to change standart output

This is a discussion on How to change standart output within the C Programming forums, part of the General Programming Boards category; I have a program that i am including in another program . I want to redirect what it prints using ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    2

    How to change standart output

    I have a program that i am including in another program . I want to redirect what it prints using printf statements to a file or a string. Can i change the standard out put of where printf prints ?

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do you mean your code is calling another executable, or you have code that you're compiling together with other code?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    2
    I have code that is compiling with another code.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Which operating system / compiler are you using for your code?
    What do you want to do "with the file" when the other program has finished?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you have the code, why don't you change the printf's to fprintf or sprintf as appropriate?

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,586
    I know how to redirect stdout to a file in C++ but I'm not sure it's C. I don't ever program in pure C.

    I use freopen() which will redirect std::out to a file. Note that in Visual Studio to redirect back to the console you must pass in "CON" as the filename. This is a little caveat of freopen() that is not mentioned in the docs for MSVS. I found it on MSDN after searching forever.

    I'm not sure if this affects printf() but if it is going to std::out internally then it will work for printf.

  7. #7
    C/C++ Learner & Lover
    Join Date
    Aug 2008
    Location
    Argentina
    Posts
    193
    You can use an output redirection on file execution, like this..
    Code:
    ./filename >> textfile.txt
    Example..
    Code:
    #include <stdio.h>
    
    int main () {
    printf("Hello World \n");
    return 0;
    }
    Will output in textfile.txt
    Code:
    Hello World
    Or you can use fprintf..
    Code:
    fprintf( stderr, "My name is PC" ); //stderr is the console, you can change it to a file, cause it's a file descriptor
    Code:
    int fprintf(FILE *file, const char *text, ...);
    Or you can also use freopen, as Bubba said..
    Code:
    FILE *freopen(const char *path, const char *mode, FILE *stream);
    Example..
    Code:
    freopen( "result.txt","wb",stdout ); // closes stdout and redirects output to result.txt
    Last edited by lautarox; 09-20-2008 at 08:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  2. help with calculating change from a dollar
    By z.tron in forum C++ Programming
    Replies: 3
    Last Post: 09-13-2002, 03:58 PM
  3. Replies: 2
    Last Post: 09-04-2001, 02:12 PM
  4. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

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