String formatting question
-
I want to do something like this, however
wsprintf
throws a compiler error. Is there another format function that I should use instead to do this?BSTR path = NULL;
int num = 1;
path = SysAllocString(L"//errors/error[@id='%i']");
wsprintf(path, num);- Nick Parker
My Blog | My Articles -
I want to do something like this, however
wsprintf
throws a compiler error. Is there another format function that I should use instead to do this?BSTR path = NULL;
int num = 1;
path = SysAllocString(L"//errors/error[@id='%i']");
wsprintf(path, num);- Nick Parker
My Blog | My Articles -
int wsprintf( LPTSTR lpOut,
LPCTSTR lpFmt,
...
);Nick Parker wrote: BSTR path = NULL; BSTR != LPCTSTR (wrong type)
int num = 1;
wsprintf(_T("//errors/error[@id='%i']");, num);"No matter where you go, there your are." - Buckaroo Banzai
-pete
palbano wrote: BSTR != LPCTSTR (wrong type) I realize that. I need to use a BSTR for the rest of what I am working on. Any other suggestions? - Nick Parker
My Blog | My Articles -
I want to do something like this, however
wsprintf
throws a compiler error. Is there another format function that I should use instead to do this?BSTR path = NULL;
int num = 1;
path = SysAllocString(L"//errors/error[@id='%i']");
wsprintf(path, num);- Nick Parker
My Blog | My ArticlesUmmm, there is a problem ... Aside from the BSTR != wchar_t*, what do you expect wsprintf to do ? I mean it's used to take a format string (path) and create a result - you haven't specified a result string. Are you expecting it to modify path in place (it won't even with correct types) ? From MSDN : typedef OLECHAR * BSTR; These strings are zero-terminated, and in most cases they can be treated just like OLECHAR* strings. However, you can query a BSTR for its length rather than scan it, so it can contain embedded null characters. The length is stored as a 32-bit integer at the memory location preceding the data in the string. The 32-bit is placed before the BSTR value so you don't need to worry about it, just cast the BSTR to a wchar_t*. e.g. (without error checking) wchar_t res[500]; BSTR path = NULL; int num = 1; path = SysAllocString(L"//errors/error[@id='%i']"); wsprintf(res, (wchar_t*)path, num); ...cmk Save the whales - collect the whole set
-
palbano wrote: BSTR != LPCTSTR (wrong type) I realize that. I need to use a BSTR for the rest of what I am working on. Any other suggestions? - Nick Parker
My Blog | My ArticlesDo all formatting using LPTSTRs, then use the
T2BSTR
conversion macro to get your BSTR type.
[
](http://www.canucks.com)Sonork 100.11743 Chicken Little "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 Within you lies the power for good - Use it!
-
I want to do something like this, however
wsprintf
throws a compiler error. Is there another format function that I should use instead to do this?BSTR path = NULL;
int num = 1;
path = SysAllocString(L"//errors/error[@id='%i']");
wsprintf(path, num);- Nick Parker
My Blog | My ArticlesYou're misusing the
BSTR
.wsprintf()
prints into aTCHAR
array, but aBSTR
is not an array.BSTR path = NULL;
CString s;
int num = 1;s.Format ( _T("//errors/error[@id='%i']"), num );
path = s.AllocSysString();If you can't/don't want to use
CString
, alloc aTCHAR
buffer instead and make sure it's big enough, then use theT2BSTR
conversion macro to convert it to aBSTR
. Check out this article[^] if you need more details on converting between string types. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- There is a saying in statistics that a million monkeys pounding on typewriters would eventually create a work of Shakespeare. Thanks to the Internet, we now know that this is not true.