Page 1 of 1

How to format string in core::stringc/w ?

Posted: Mon May 28, 2007 5:01 pm
by greenya
I want to use only core::stringc/w , so can I do something like this ?:

Code: Select all

core::stringc a;
a.format( "count=%i", 10 );
if I will use char* I would wrote that in next way:

Code: Select all

char s[100];
sprintf( s, "count=%i", 10 );
But I want to do the same using only core::stringc/w, so the question is: how correctly (platform independently) format strings of types core:stringc/w ?

Posted: Mon May 28, 2007 6:20 pm
by LLDD
Something like this should work:

Code: Select all

stringc a;
a.reserve(100);
sprintf(a.c_str(), "Count: %d", 10);
________
Guaranteed Asset Protection Insurance Advice

Posted: Mon May 28, 2007 8:13 pm
by hybrid
OMG, i hope not :shock: No, of course it won't work. c_str() returns a const char* string which is not writable. You'll have to use append and stuff to create your string. We do not have variable argument methods in Irrlicht, yet.

Posted: Sat Jun 02, 2007 11:43 am
by lexchou

Code: Select all

template<class T>
irr::core::string<T> format(const T * fmt,...)
 {
	 va_list va;
	 va_start(va,fmt);
	 T buffer[300];
	 _vstprintf(buffer,fmt,va);
	 va_end(va);
	 return buffer; 
 }
you can try this, but you need to include tchar.h first

examples:

Code: Select all

irr::core::stringw test=format(L"a=%d",1);
irr::core::stringc test=format("a=%d",1);

Posted: Sat Jun 02, 2007 3:29 pm
by greenya
lexchou,
nice, Thank You.

Posted: Sat Jun 02, 2007 3:51 pm
by hybrid
Yeah, indeed a nice function. But Windows-only. I think for a proper solution in Irrlicht we will need template specialization here... We have to use vsnprintf or vsnwprintf (resp. the _vs... versions from Microsoft). I wonder if VC6 will do this.