Reporting formatted errors

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

    [cpp]

    #include
    //—–blah blah

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
    AnsiString filename=”c:\\mytestfile.txt”;

    if (!FileExists(filename))
    ShowError(“Cannot open file %s”,filename.c_str());
    }

    void __cdecl TForm1::ShowError(const char *format, …)
    {
    va_list args;

    va_start(args,format);

    AnsiString Buf=”";
    Buf.vprintf(format,args);
    va_end(args);

    Application->MessageBoxA(Buf.c_str(),”ERROR”,MB_OK | MB_ICONERROR);
    }
    [/cpp]

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

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>