Click here to Skip to main content
Licence 
First Posted 26 Nov 2004
Views 23,211
Bookmarked 13 times

Replacing printf for use with MFC

By | 26 Nov 2004 | Article
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

About the Author

Manohar B. Srikanth

Web Developer

India India

Member

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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalthx.. worked right off the bat :) Pinmembernat2kus18:00 7 Jun '07  
QuestionHow to set your code up. [modified] Pinmembercompucoach17:52 13 Jun '06  
GeneralNeed Help Pinmemberjagadishgouda2:10 31 Jan '06  
Generalsmall problem PinmemberMystic_Unicorn22:25 23 Apr '05  
GeneralRe: small problem PinmemberManohar B. Srikanth1:32 8 Jul '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 26 Nov 2004
Article Copyright 2004 by Manohar B. Srikanth
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid