Raz-Soft » Blog Archive » Reporting formatted errors
Nu călcaţi pe iarbă »
« Two words for Two Worlds game…

Reporting formatted errors

So, you want to report errors in a printf / sprintf way? Check this out...

    C++:
    1. #include <stdarg.h>
    2. //-----blah blah
    3.  
    4. void __fastcall TForm1::Button1Click(TObject *Sender)
    5. {
    6.    AnsiString filename="c:\\mytestfile.txt";
    7.  
    8.    if (!FileExists(filename))
    9.     ShowError("Cannot open file %s",filename.c_str());
    10. }
    11.  
    12.  
    13. void __cdecl TForm1::ShowError(const char *format, ...)
    14. {
    15.   va_list args;
    16.  
    17.   va_start(args,format);
    18.  
    19.   AnsiString Buf="";
    20.   Buf.vprintf(format,args);
    21.   va_end(args);
    22.  
    23.   Application->MessageBoxA(Buf.c_str(),"ERROR",MB_OK | MB_ICONERROR);
    24. }

ps: don't forget to declare void __cdecl TForm1::ShowError(const char *format, ...) in your TForm class ;)

related things:

Author: Raz | On June 14th, 2007 | C++ Builder, [ En ] | No Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions