Michael Knight
asked on
Can you pass a va_list as arguments to fill a CString (variable argument list)?
I have a class member that accepts a string with formatting capability, and therefore can have multiple arguments to fill the format sections. Can I simply take the string I get and the argument list and send it to CString and let it do the work of stuffing? How can I do this. I want to take the formatted string and send it to an open I/O stream. I'm using MSVC++ 4.2 under Win95 and WinNT 4.x. (Must I just add my own function like the protected CString::FormatV)?)
Here's my function member:
////////////////////////// ////////// ////////// ////////// ////////// ////////// ////////// ////////// /
// this function member will extract the arguments into a va_list, set the new string, and
// append the string to the open report file
// it will display an error if a file has not been opened first with OpenReportFile
// this function will allow the passing of arguments as used in the printf and
// CString::Format functions (see Microsoft VC++ help for information on formatting codes)
//
// Input Variables: lpszformatstr - string of text to be written to open file w/formatting info
// ... - formatting variable argument list
//
// Output Variable: function status return (TRUE- success, FALSE- error)
BOOL KEMETReportClass::WriteLin eToReportF ile(LPCTST R lpszformatstr, ...)
{
// assure a good valid string
ASSERT(AfxIsValidString(lp szformatst r,FALSE));
// check that a file is actually open
if (!FileOpened || reportfilehandle == NULL)
{
AfxMessageBox("KEMETReport Class Warning!\nNo File Has Been Opened.\nCannot Write Line.",MB_OK | MB_ICONEXCLAMATION);
return(FALSE);
}
// stuff the text string into a formatted CString
CString ReportLineTextString;
va_list arglist;
va_start(arglist,lpszforma tstr);
ReportLineTextString.Forma t("%s",lps zformatstr ,arglist);
va_end(arglist);
// now check for empty string
// if not empty, spit it to the file, othewise, display a NULL string error message
if (!ReportLineTextString.IsE mpty())
fprintf(reportfilehandle," %s",Report LineTextSt ring);
else
{
AfxMessageBox("KEMETReport Class Warning!\nCannot Write A NULL Text Line.",MB_OK | MB_ICONEXCLAMATION);
return(FALSE);
}
AfxMessageBox(ReportLineTe xtString);
return(TRUE);
}
Here's my function member:
//////////////////////////
// this function member will extract the arguments into a va_list, set the new string, and
// append the string to the open report file
// it will display an error if a file has not been opened first with OpenReportFile
// this function will allow the passing of arguments as used in the printf and
// CString::Format functions (see Microsoft VC++ help for information on formatting codes)
//
// Input Variables: lpszformatstr - string of text to be written to open file w/formatting info
// ... - formatting variable argument list
//
// Output Variable: function status return (TRUE- success, FALSE- error)
BOOL KEMETReportClass::WriteLin
{
// assure a good valid string
ASSERT(AfxIsValidString(lp
// check that a file is actually open
if (!FileOpened || reportfilehandle == NULL)
{
AfxMessageBox("KEMETReport
return(FALSE);
}
// stuff the text string into a formatted CString
CString ReportLineTextString;
va_list arglist;
va_start(arglist,lpszforma
ReportLineTextString.Forma
va_end(arglist);
// now check for empty string
// if not empty, spit it to the file, othewise, display a NULL string error message
if (!ReportLineTextString.IsE
fprintf(reportfilehandle,"
else
{
AfxMessageBox("KEMETReport
return(FALSE);
}
AfxMessageBox(ReportLineTe
return(TRUE);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.