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.
eeuserexAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
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));
0
eeuserexAuthor Commented:
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?
0
nietodCommented:
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";
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

clarkaCommented:
bstr_t s, m_bstrName;
m_bstrName = _T("MyName");

s = _T("<FORM METHOD=GET ACTION=") + m_bstrName + _T(">\r\n");

Regards
Allan C..
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
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!
0
chensuCommented:
>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?
0
nietodCommented:
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.
0
eeuserexAuthor Commented:
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?
0
chensuCommented:
Haven't you seen the Reject Answer option?
0
nietodCommented:
That wasn't the reject option.
0
chensuCommented:
eeuserex,

Didn't you recognize "Reject" and "Accept"?
0
eeuserexAuthor Commented:
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.
0
nietodCommented:
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.
0
clarkaCommented:
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.
0
clarkaCommented:
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.
0
nietodCommented:
>> 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.  
0
clarkaCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.