Jump to content
  • Advertisement
Sign in to follow this  
lordoftools

string to CString

This topic is 4683 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Right now im working on a way to find the path of the application that is running. Some members of this forum have given me some code that i wanted to try out but the path is stored in a string (std::basic_string according to the compiler) but in order to use that in any means that i'd like such as with cout<< so that i can be sure it's giving me the right path i need it to be a CString or a const *char. Can anyone help me convert these variables? Code for my app incase it helps:
#include "stdafx.h"
#include <cstring>
#include <string>
#include <conio.h>
#include <iostream.h>
#include <afx.h>
//#include <stdio>

using std::string;
using std::wstring;

wstring get_module_path_wide(void);
string wsts(const wstring &src_string);

int main(int argc, char* argv[])
{
	string strPath;
        CString cstrPath;

        //this line appears to get all the proper data but can't check it _
        //until i can use it in cout or prinf or something like that which _
        //needs CString or const *char.
	strPath=wsts(get_module_path_wide());

        //this line will error out each time but that is the basic _
        //task that needs to be done, strPath turns into cstrPath.

	//cstrPath=strPath;

        //possible display routines that i'd like to use

        //cout<<cstrPath;
        //printf(cstrPath);
		
	return 0;
}


// use Unicode (wide) version of GetModuleFileName
wstring get_module_path_wide(void)
{
	wchar_t wide_buf[65536];
	wstring module_name;

	if(!GetModuleFileNameW(GetModuleHandle(0), wide_buf, 65535))
		return module_name;

	wide_buf[65535] = L'\0';

	module_name = wide_buf;

	size_t pos = module_name.find_last_of(L'\\');

	if(pos != string::npos)
		module_name = module_name.substr(0, pos);

	return module_name;
}

// use this function to convert from wide wstring to ASCII string if need be
string wsts(const wstring &src_string)
{
	size_t src_len = src_string.length();

	if(src_len == 0)
		return "";

	char *buf = new(std::nothrow) char[src_len + 1];

	if(buf == 0)
		return "";

	wcstombs(buf, src_string.c_str(), src_len);
	buf[src_len] = '\0';

	string final_string = buf;

	if(buf != 0)
		delete [] buf;

	return final_string;
}


edit: added source tags -SiCrane

Share this post


Link to post
Share on other sites
Advertisement
To convert basic_string to a const char*, you just do this:-



string strString = "Hello";
const char* strConstChar = NULL;

strConstChar = strString.data( ); // or strString.c_str( );


Share this post


Link to post
Share on other sites
Quote:
Original post by rpg_code_master
To convert basic_string to a const char*, you just do this:-

*** Source Snippet Removed ***

Use c_str(), not data().

data() only points to the string's internal buffer which may not be null-terminated.
c_str() returns a pointer to a string that is *guaranteed* to be null-terminated.

Share this post


Link to post
Share on other sites
Oh, come on! Use <iostream>, not <iostream.h> and you don't have to convert anything. The former is standard and correct, the latter is pre-standard and can pretty much do whatever it pleases.

Use the standard headers!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!