Click here to Skip to main content
15,914,500 members
Articles / Programming Languages / C++
Article

Replacing printf for use with MFC

Rate me:
Please Sign up or sign in to vote.
2.53/5 (12 votes)
26 Nov 2004 34.4K   13   5
Replacing printf for use with MFC.

Introduction

This article explains a simple method to replace the printf function calls within an MFC application.

Background

Quite often we find very useful and well written code with lots of printf function calls. When we port this code into a MFC based GUI application we would certainly like the output of the printf to go into a listbox or a message box. Replacing a printf with a message box or list box display would take only a few lines of code. This is too hard especially if there are 100's of printfs all over.

Using the code

It would be very nice to replace all the printf calls with MFC_Printf in one shot just by pressing Ctrl+H shortcut, that is find and replace. Here is the construction of MFC_Printf(....):

class CMFCLog {
{
public:
  CMFCLog();
  {
  
  }
  ~CMFCLog();
  {

  }
  CListBox *m_pLB; // pointer to a CListBox

  void MFC_Printf(const char *formatstring,...)
  {
       CString    Str, s2;
    va_list    args;

    va_start(args, str);
              
    Str.FormatV(formatstring, args);

    Str.Replace('\n',' ');

    // Assuming that m_pLB is already initialized.
     m_pLB->AddString(Str);
     
    // We can even call a MessageBox here...
    
}
};
// This code is within the CPP file which contain lots of printf
static CMFCLog    Log;

....... Assign the m_pLB some where before calling MFC_Printf


// printf("\n\nThe Counter Values is: %d",i); is replaced with
Log.MFC_Printf("\n\nThe Counter Values is: %d", i);

Points of interest

We should always find simple solutions.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
Me!!?, I am Research Student at the Computer Grpahics & Visualization lab of SERC, Indian Institute of Science.

-- Manohar
Bangalore, INDIA

http://ssl.serc.iisc.ernet.in/~manohar/
www.scientifik.org

Comments and Discussions

 
Generalthx.. worked right off the bat :) Pin
nat2kus7-Jun-07 18:00
nat2kus7-Jun-07 18:00 
Smile | :)
QuestionHow to set your code up. [modified] Pin
compucoach13-Jun-06 17:52
compucoach13-Jun-06 17:52 
GeneralNeed Help Pin
jagadishgouda31-Jan-06 2:10
jagadishgouda31-Jan-06 2:10 
Generalsmall problem Pin
Mystic_Unicorn23-Apr-05 22:25
Mystic_Unicorn23-Apr-05 22:25 
GeneralRe: small problem Pin
Manohar B. Srikanth8-Jul-05 1:32
Manohar B. Srikanth8-Jul-05 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.