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

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

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

Post 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 ?
LLDD
Posts: 43
Joined: Fri May 11, 2007 9:50 am

Post 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
Last edited by LLDD on Sun Feb 20, 2011 2:13 am, edited 1 time in total.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
lexchou
Posts: 2
Joined: Fri Mar 23, 2007 3:17 pm
Location: China

Post 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);
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

lexchou,
nice, Thank You.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post 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.
Post Reply