How to convert string to const char[] in c++ - string

I am working on a C++ project for a Vex Robot, I am using a function that takes a const char[] but for showing an integer, there is no such function that converts to const char[], so is there a way to do that. All the search results showed how to convert to const char* and by the way "string" is of type const char[].
EDIT:
I also need to covert the string to concatenate tow strings (but ended up just coverting both strings to std::string and then back to const char[])

With std::string there is the function c_str() (with the synonym data() since C++11) to get a pointer to the underlying null-terminated character array:
std::string const myString{"stackoverflow"};
const char* cstr = myString.c_str();
const char* data = myString.data();

Related

CString to const char * _Source in function strcpy_s

I have code:
char buff[50000];
CString params = "sss" ;
strcpy_s(buff,params);
Why strcpy_s accepts second param that is CString? According to my understanding second param should be of type const char * _Source
CString has a LPCTSTR conversion operator. The compiler calls this to convert your CString to the required const char *. So what the compiler does is:
strcpy_s(buff, (const char*)params);
which is equivalent to
strcpy_s(buff, (LPCTSTR)params);
which is like
strcpy_s(buff, params.operator LPCTSTR());
This only works if you are not compiling for Unicode. On Unicode LPCTSTR is not const char * but const WCHAR *, so this would not work.

How to convert char* filename to string?

How can i convert a char* filename to a string type in C++ .
I'm working on file handles .One instance is of process and other one is a filename .
For comparison purpose i need to convert both to String Type.
if(_stricmp(pEntry32.szExefile,filename) ==0)
{ //HANDLE stuff
}
this module wOrks fine on DEV C++ but not on VS 2008 .
The std::string class has a constructor that takes a c string.
std::string process_str = std::string(pEntry32.szExefile);
std::string filename_str = std::string(filename);

How to convert string to LPSTR in WinAPI function which stores output in string

I am trying to store some contents into a string variable by passing it as a parameter in various types of Windows API functions which accepts variable like char *.
For example, my code is:-
std::string myString;
GetCurrentDirectoryA( MAX_PATH, myString );
Now how do I convert the string variable to LPSTR in this case.
Please see, this function is not meant for passing the contents of string as input, but the function stores some contents into string variable after execution. So, myString.c_str( ) is ruled out.
Edit: I have a workaround solution of removing the concept of string and replacing it with something like
char myString[ MAX_PATH ];
but that is not my objective. I want to make use of string. Is there any way possible?
Also casting like
GetCurrentDirectoryA( MAX_PATH, ( LPSTR ) myString );
is not working.
Thanks in advance for the help.
Usually, people rewrite the Windows functions they need to be std::string friendly, like this:
std::string GetCurrentDirectoryA()
{
char buffer[MAX_PATH];
GetCurrentDirectoryA( MAX_PATH, buffer );
return std::string(buffer);
}
or this for wide char support:
std::wstring GetCurrentDirectoryW()
{
wchar_t buffer[MAX_PATH];
GetCurrentDirectoryW( MAX_PATH, buffer );
return std::wstring(buffer);
}
LPTSTR is defined as TCHAR*, so actually it is just an ordinary C-string, BUT it depends on whether you are working with ASCII or with Unicode in your code. So do
LPTSTR lpStr = new TCHAR[256];
ZeroMemory(lpStr, 256);
//fill the string using i.e. _tcscpy
const char* cpy = myString.c_str();
_tcscpy (lpStr, cpy);
//use lpStr
See here for a reference on _tcscpy and this thread.
Typically, I would read the data into a TCHAR and then copy it into my std::string. That's the simplest way.

Conversion of CString to float

Some body help me regarding to the following problem
strFixFactorSide = _T("0.5");
dFixFactorSide = atof((const char *)(LPCTSTR)strFixFactorSide);
"dFixFactorSide" takes value as 0.0000;
How I will get correct value?
Use _tstof() instead of atof(), and cast CString to LPCTSTR, and leave it as such, instead of trying to get it to const char *. Forget about const char * (LPCSTR) while you're working with unicode and use only const _TCHAR * (LPCTSTR).
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
CString s1 = _T("123.4");
CString s2 = _T("567.8");
double v1 = _tstof((LPCTSTR)s1);
double v2 = _tstof((LPCTSTR)s2);
_tprintf(_T("%.3f"), v1 + v2);
return nRetCode;
}
and running this correctly gives the expected answer.
I think your CString strFixFactorSide is a Unicode (UTF-16) string.
If it is, the cast (const char *) only changes the pointer type, but the string it points to still remains Unicode.
atof() doesn't work with Unicode strings. If you shove L"0.5" into it, it will fetch bytes 0x30 ('0') and 0x00 (also part of UTF-16 '0'), treat that as a NUL-terminated ASCII string "0" and convert it to 0.0.
If CString strFixFactorSide is a Unicode string, you need to either first convert it to an ASCII string and then apply atof() or use a function capable of converting Unicode strings to numbers. _wtof() can be used for Unicode strings.

boost::interprocess::string conversion to char*

Is it possible to convert a boost::interprocess::string to an std::string or to a const char*? Something like c_str()...
E.g.:
boost::interprocess::string is = "Hello world";
const char* ps = is.c_str(); // something similar
printf("%s", ps);
I could even get a copy of the string in a non-shared memory block.
E.g.:
boost::interprocess::string is = "Hello world";
const char cs[100];
strcpy(cs, is.c_str()); // something similar
printf("%s", cs);
Thank you!
boost::interprocess::string has a standard c_str() method. I found the following here:
//! <b>Returns</b>: Returns a pointer to a null-terminated array of characters
//! representing the string's contents. For any string s it is guaranteed
//! that the first s.size() characters in the array pointed to by s.c_str()
//! are equal to the character in s, and that s.c_str()[s.size()] is a null
//! character. Note, however, that it not necessarily the first null character.
//! Characters within a string are permitted to be null.
const CharT* c_str() const
{ return containers_detail::get_pointer(this->priv_addr()); }
(That's for the basic_string. string is a template instantiation in which the CharT template parameter is char.)
Also, the documentation here says
basic_string is the implementation of
std::basic_string ready to be used in
managed memory segments like shared
memory. It's implemented using a
vector-like contiguous storage, so it
has fast c string conversion...

Resources