coverting from CString to char

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
C-lover-1
Posts: 12
Joined: Sat Feb 17, 2007 8:46 pm

coverting from CString to char

Post by C-lover-1 »

hello all.
i have problem in loading irr file to my mfc app.
for loading irr file i have to use COpendialog .
but it's works with CString and loadscene() works with char .
how can i solve this problem ( converting Cstring to char ).
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

What is CString (and what is COpenDialog?)? If you meant stringc you can use the .c_str() method from irrStrings.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

They are MFC classes. You should really be looking at the MFC documentation to find the answer. That said, the MFC CString class provides a conversion operator to LPCTSTR which is actually a const TCHAR*. Unless you are compiling your code with _UNICODE defined a TCHAR is actually a char.

So anywhere that you need a const char* you can just pass a CString. The necessary conversion will happen automatically.

Travis
C-lover-1
Posts: 12
Joined: Sat Feb 17, 2007 8:46 pm

Post by C-lover-1 »

thankyou vitek.


forget MFC . i am using single win32 app

i just got these resualts

Code: Select all

	//first code
	CFileDialog open(TRUE);
	const TCHAR* filename;
	if (open.DoModal() == IDOK)
	{
	filename = open.GetFileName();
	}
	Smgr->loadScene((irr::c8*)filename);
resualt is many unknown characters and also engine log : unable to load scene file + unknown characters.


Code: Select all

	// second code
	CFileDialog open(TRUE);
	CString cstr;
	if (open.DoModal() == IDOK)
	{
	cstr = open.GetFileName();
	}
	Smgr->loadScene((irr::c8*)(const TCHAR*)cstr);
resualt : first character of the filename . (example : file name = scene1.irr -> resualt = s)
and also engine log : unable to open scene file :s

i can not load my scene yet .
if it is possible please help me in this case .
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

The first method you posted won't ever work. The string that is being returned is a temporary, and you are getting a pointer to something that is owned by that temporary. When the temporary goes out of scope, it is destroyed, and your pointer points to garbage.

Theoretically, you should only have to do this...

Code: Select all

CFileDialog open(TRUE); 
if (open.DoModal() == IDOK) 
{ 
   Smgr->loadScene(open.GetFileName());
} 
Given your last post, I'm assuming that you will get an error message about an illegal conversion from TCHAR* or wchar_t* to c8*. If this is the case, then I'm guessing that you are compiling with _UNICODE defined. If the symbol _UNICODE is defined for a build of your program, TCHAR is defined as type wchar_t, otherwise it is just a char.

The easy solution is to remove the definition of _UNICODE. It usually comes from your project settings, but it can come from other places. If you don't want to remove _UNICODE, you need to convert the wchar_t string to a char string. You can do this with core::stringc or with wcstomb.

Code: Select all

CFileDialog open(TRUE); 
if (open.DoModal() == IDOK) 
{ 
#ifdef _UNICODE
   // use stringc conversion ctor to do cheap conversion
   core::stringc filename(open.GetFileName());
   Smgr->loadScene(filename.c_str());
#else
   Smgr->loadScene(open.GetFileName());
#endif
} 
C-lover-1
Posts: 12
Joined: Sat Feb 17, 2007 8:46 pm

Post by C-lover-1 »

i think UNICODE has defined before , becuase when i defined it , i got warning for redefinition UNICODE.
this is error that compiler gave me after i replace them with your code:

Code: Select all

 error C2664: 'irr::core::string<T>::string(const irr::core::string<T> &)' : cannot convert parameter 1 from 'CString' to 'const irr::core::string<T> &'
        with
        [
            T=wchar_t
        ]
        Reason: cannot convert from 'CString' to 'const irr::core::string<T>'
        with
        [
            T=wchar_t
        ]
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

//error is for this line :core::stringw filename(open.GetFileName());

Code: Select all

error C2664: 'bool irr::scene::ISceneManager::loadScene(const irr::c8 *,irr::scene::ISceneUserDataSerializer *)' : cannot convert parameter 1 from 'const wchar_t *' to 'const irr::c8 *'
        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

 //error is for this line :Smgr->loadScene(filename.c_str());
unfortunately i don't know why you can compile it but i can't.
and worst thing is that when i ignore other code and use just this line of code : Smgr->loadScene(open.GetFileName()); ,

i get this error :

Code: Select all

error C2664: 'bool irr::scene::ISceneManager::loadScene(const irr::c8 *,irr::scene::ISceneUserDataSerializer *)' : cannot convert parameter 1 from 'CString' to 'const irr::c8 *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
thankyou for your attention vitek.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

i think UNICODE has defined before , becuase when i defined it , i got warning for redefinition UNICODE.
Like I said, if it is defined, the easiest solution is to remove it.

From the first error message you gave, it looks like you used core::stringw, but you need to use core::stringc. Try this...

Code: Select all

// note that this is a stringc, not a stringw!!!
core::stringc filename(open.GetFileName().GetBuffer());
Smgr->loadScene(filename.c_str());
Travis
C-lover-1
Posts: 12
Joined: Sat Feb 17, 2007 8:46 pm

Post by C-lover-1 »

thankyou very much Travis.
problem solved with your last code .
thankyou :D :D :D :D :D :D
Post Reply