Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Redirecting From stdout

sandeept_24sandeept_24 Posts: 8Member
I want to redirect output of printf and perror to a file in a C program. Please anyone help me how to do it. I also tried this by the help of system() funtion but it did not work.
For example i want to print the messages below to a file without using fprintf. Even if the file is opened.

fp=fopen("abc.txt","w");

printf("
Hai How are You ?
");
perror("
I AM HERE
");


I want that both the above statements should write into the file abc.txt not at stdout or stderr ie console.
Please help. Thanks


Comments

  • tsagldtsagld Posts: 621Member
    : I want to redirect output of printf and perror to a file in a C program. Please anyone help me how to do it. I also tried this by the help of system() funtion but it did not work.
    : For example i want to print the messages below to a file without using fprintf. Even if the file is opened.
    :
    : fp=fopen("abc.txt","w");
    :
    : printf("
    Hai How are You ?
    ");
    : perror("
    I AM HERE
    ");
    :
    :
    : I want that both the above statements should write into the file abc.txt not at stdout or stderr ie console.
    : Please help. Thanks
    :
    :
    Use the freopen function:
    [code]
    stream = freopen( "abc.tct", "w", stdout );
    [/code]

    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • stoberstober Posts: 9,765Member ✭✭✭
    [b][red]This message was edited by stober at 2006-5-19 7:46:32[/red][/b][hr]
    : I want to redirect output of printf and perror to a file in a C program. Please anyone help me how to do it. I also tried this by the help of system() funtion but it did not work.
    : For example i want to print the messages below to a file without using fprintf. Even if the file is opened.
    :
    : fp=fopen("abc.txt","w");
    :
    : printf("
    Hai How are You ?
    ");
    : perror("
    I AM HERE
    ");
    :
    :
    : I want that both the above statements should write into the file abc.txt not at stdout or stderr ie console.
    : Please help. Thanks
    :
    :
    :


    you are using the wrong function.
    fprintf(fp,"
    Hai How are You ?
    ");

    perror goes to stderr, not to a file. So you have to call freopen() as previously suggested.
    stream = freopen( "abc.tct", "w", stderr );

    see the example program here:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_freopen.2c_._wfreopen.asp




  • tsagldtsagld Posts: 621Member
    : [b][red]This message was edited by stober at 2006-5-19 7:46:32[/red][/b][hr]
    : : I want to redirect output of printf and perror to a file in a C program. Please anyone help me how to do it. I also tried this by the help of system() funtion but it did not work.
    : : For example i want to print the messages below to a file without using fprintf. Even if the file is opened.
    : :
    : : fp=fopen("abc.txt","w");
    : :
    : : printf("
    Hai How are You ?
    ");
    : : perror("
    I AM HERE
    ");
    : :
    : :
    : : I want that both the above statements should write into the file abc.txt not at stdout or stderr ie console.
    : : Please help. Thanks
    : :
    : :
    : :
    :
    :
    : you are using the wrong function.
    : fprintf(fp,"
    Hai How are You ?
    ");
    :
    : perror goes to stderr, not to a file. So you have to call freopen() as previously suggested.
    : stream = freopen( "abc.tct", "w", stderr );
    :
    : see the example program here:
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_freopen.2c_._wfreopen.asp
    :
    :
    :
    :
    He mentioned that he didn't want to use fprintf. So I figured he knows that function and has some argument not to use it.



    Greets,
    Eric Goldstein
    http://www.gvh-maatwerk.nl


  • stoberstober Posts: 9,765Member ✭✭✭
    : He mentioned that he didn't want to use fprintf. So I figured he knows that function and has some argument not to use it.
    :

    this seems to work
    [code]
    #include
    #include

    int main()
    {
    FILE* stream1 = freopen( "d:\abc.txt", "w", stdout );
    FILE* stream2 = freopen( "d:\abc.txt", "a+", stderr );
    printf("Hello there
    ");
    perror(NULL);
    fclose(stream1);
    fclose(stream2);
    return 0;
    }
    [/code]
Sign In or Register to comment.