C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28629
Number of posts: 94611

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Redirecting From stdout Posted by sandeept_24 on 19 May 2006 at 5:43 AM
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("\nHai How are You ?\n");
perror("\n I AM HERE\n");


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


Report
Re: Redirecting From stdout Posted by tsagld on 19 May 2006 at 7:27 AM
: 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("\nHai How are You ?\n");
: perror("\n I AM HERE\n");
:
:
: 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:
stream = freopen( "abc.tct", "w", stdout );


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


Report
Re: Redirecting From stdout Posted by stober on 19 May 2006 at 7:40 AM
This message was edited by stober at 2006-5-19 7:46:32

: 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("\nHai How are You ?\n");
: perror("\n I AM HERE\n");
:
:
: 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,"\nHai How are You ?\n");

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




Report
Re: Redirecting From stdout Posted by tsagld on 19 May 2006 at 8:17 AM
: This message was edited by stober at 2006-5-19 7:46:32

: : 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("\nHai How are You ?\n");
: : perror("\n I AM HERE\n");
: :
: :
: : 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,"\nHai How are You ?\n");
:
: 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


Report
Re: Redirecting From stdout Posted by stober on 19 May 2006 at 1:15 PM
: 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
#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE* stream1 = freopen( "d:\\abc.txt", "w", stdout ); 
    FILE* stream2 = freopen( "d:\\abc.txt", "a+", stderr ); 
    printf("Hello there\n");
    perror(NULL);
    fclose(stream1);
    fclose(stream2);
     return 0;
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.