Link to home
Start Free TrialLog in
Avatar of eeuserex
eeuserex

asked on

String formatting without MFC (how?)

How can you format a string without using sprintf? Or How can dynamically (runtime) determine the buffer size?
 
Example:
std::string str;
char buffer(SIZE);
sprintf(buffer,"<FORM METHOD=GET ACTION=\"%s\">\r\n", OLE2T(m_bstrName));
  str = buffer;

------

With MFC you can use cstring e.g. str.format but this is an ATL only project. So any help would be appreciated.
Avatar of chensu
chensu
Flag of Canada image

Since it is a Windows program, using wsprintf is better. You have to estimate the buffer size needed.

const int nSize = 128;
TCHAR pszBuf[nSize];
wsprintf(pszBuf, _T("<FORM METHOD=GET ACTION=\"%s\">\r\n"), OLE2T(m_bstrName));
Avatar of eeuserex
eeuserex

ASKER

The question is really about how we can either dynamically define the buffer or format another way so I do not belive this answer applies.  Any other suggestions?
STL string's can be formatted using their member functions, for example you code above could be formatted using

std::string str;
str = "<FORM METHOD=GET ACTION=\"" + OLE2T(m_bstrName) + "\">\r\n";
ASKER CERTIFIED SOLUTION
Avatar of clarka
clarka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
clarka, Did you read the question history?  That is exactly what I proposed,  except I used a STL string, instead of a bstr_t  because that is eeuserex is using!
>std::string str;
>str = "<FORM METHOD=GET ACTION=\"" + OLE2T(m_bstrName) + "\">\r\n";

But this is limited to strings only. What about other formating supported by sprintf?
It handles the example, its hard to say what else eeuserex needs to do.  There are many other string operations that can help in formatting strings.  None for handling numbers though.

If you need to convert an expression using numbers to a string you can use a string stream.  You use operator << to output the numbers in the correct format to the stream along with any text or other things you want in the string.  Then you retreive the string from the stream.
Thanks for the answer.  nietod responded first and should get the credit however it is only allowing me to accept clarka's proposed answer.  How should I close this off?
Haven't you seen the Reject Answer option?
That wasn't the reject option.
eeuserex,

Didn't you recognize "Reject" and "Accept"?
Yes, however since nietod had only made a comment I would not have been able to accept this as an answer.  Since clarka's answer was the first correct answer proposed I accepted it.  Thanks for your assistance.  If there was an alternative to close this off please let me know so I can do so for future threads.
Only one expert can answer at a time.  So you should have rejected the  answer that was pending.  Then another expert could answer OR you could then accept any of the comments listed as answers.
There was an option to withdrawal my answer, which I did, I don't know if the status was updated before eeuserex excepted the answer.  

In regards to comment: (clarka, Did you read the question history.)
I did, I posted an ATL solution which the question specifically asks for not stl one.

eeuserex, you will need to post a question to the system admins to move the point to nietod.
There was an option to withdrawal my answer, which I did, I don't know if the status was updated before eeuserex excepted the answer.  

In regards to comment: (clarka, Did you read the question history.)
I did, I posted an ATL solution which the question specifically asks for not stl one.

eeuserex, you will need to post a question to the system admins to move the point to nietod.
>> I don't know if the status was updated before
>> eeuserex excepted the answer
Apparently it wasn't, as the feature (which is new) is working, or at least has worked in other cases.

>>  I posted an ATL solution which the question
>> specifically asks for not stl one
His point was that he didn't want any MFC-only code.  (MFC has stuff for this.)  He is using STL, his code uses an STL string, not a BSTR.

>> you will need to post a question to the system
>> admins to move the point to nietod.
Thanks, but its not worth bothering them for.  
netiod the questions says:
>>With MFC you can use cstring e.g. str.format but this is an ATL only project. So any help would be >>appreciated.

ATL only project, you should have re-read the question yourself.