News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Redirct stdout to file

Started by JohnF, August 01, 2007, 07:58:40 AM

Previous topic - Next topic

JohnF

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

xanatose

you can also use

freopen("file.txt","wt",stdout);