Pelles C forum

C language => Tips & tricks => Topic started by: JohnF on August 01, 2007, 07:58:40 AM

Title: Redirct stdout to file
Post by: JohnF on August 01, 2007, 07:58:40 AM
When debugging a large project it is sometimes convenient to print to a file. Instead of using fprintf and carrying around a file handle you can redirect stdout to a file and use printf.

As an example, at the top of WinMain

    fclose(stdout);
    *stdout = *fopen("c:\\debug.log", "w");

Then use printf throughout the project.

stdout is closed on exit but one could close it manually before one exits.

    fclose(stdout);

I don't know if this is considered bad practice but it works.

John
Title: Re: Redirct stdout to file
Post by: xanatose on November 03, 2008, 10:18:46 PM
you can also use
Code: [Select]
freopen("file.txt","wt",stdout);