[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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.
0
eeuserex
Asked:
eeuserex
  • 6
  • 4
  • 4
  • +1
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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
 
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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now