?
Solved

Can you pass a va_list as arguments to fill a CString (variable argument list)?

Posted on 1997-05-29
1
Medium Priority
?
1,147 Views
Last Modified: 2013-12-03
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::WriteLineToReportFile(LPCTSTR lpszformatstr, ...)
{
      // assure a good valid string
      ASSERT(AfxIsValidString(lpszformatstr,FALSE));

      // check that a file is actually open
      if (!FileOpened || reportfilehandle == NULL)
            {
            AfxMessageBox("KEMETReportClass 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,lpszformatstr);
      ReportLineTextString.Format("%s",lpszformatstr,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.IsEmpty())
            fprintf(reportfilehandle,"%s",ReportLineTextString);
      else
            {
            AfxMessageBox("KEMETReportClass Warning!\nCannot Write A NULL Text Line.",MB_OK | MB_ICONEXCLAMATION);
            return(FALSE);
            }
      
AfxMessageBox(ReportLineTextString);
      return(TRUE);
}
0
Comment
Question by:maknight
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 6

Accepted Solution

by:
jpk041897 earned 30 total points
ID: 1397711
valists separate strings from eachoter using nulls, for instance:

"String 1"\0"String 2"\0"String 3"\0\0

Deafault constructors for CString would only read the first string in the va list.

You would either have to derive a class from CString and add your own constructor or add a method that woud parse the va list and implement the functionality you desire.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to make a Windows 7 gadget that accepts files dropped from the Windows Explorer.  It also illustrates how to give your gadget a non-rectangular shape and how to add some nifty visual effects to text displayed in a your gadget.…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question