I'm using MS VC++ 6.0 to create a data import tool to go from a text file into a specialised database.
Anyway, I have a bunch of data records with their values stored in CString arrays, but I need to pass one of those fields as a char into a function.
I'm just having one of those days where I know the answer is simple, and I will kick myself after (picture may be provided as evidence), but I just got a mental block right now.
Any help would be appreciated...

Mental Block - CString to Char
Peng Song
Bye
Martin
Malika Senaratna
CString Operations Relating to C-Style Strings
Converting to C-Style Null-Terminated Strings
http://msdn.microsoft.com/library/default.asp url=/library/en-us/vccore98/HTML/_core_strings.3a_.cstring_operations_relating_to_c.2d.style_strings.asp
This is the published microsoft example:
CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);
//... modify lpsz as much as you want
I would have done it like this:
// #define _UNICODE
#include <afx.h>
#include <stdio.h>
#include <tchar.h>
void main( )
{
// CString theString("This is a test" ); // converts to UNICODE string internally
// CString theString(L"This is a test" ); // stores a UNICODE string literal
CString theString(_T( "This is a test") ); // passes literal string based on UNICODE flag
// LPTSTR lpsz = new TCHAR[theString.GetLength()+1]; // optional way
// LPTSTR lpsz = new _TCHAR[theString.GetLength()+1]; // optional way
// _TCHAR *mytchar = new _TCHAR[theString.GetLength()+1]; // optional way
TCHAR *mytchar = new TCHAR[theString.GetLength()+1];
_tcscpy(mytchar, theString);
_tprintf("mytchar=%s", mytchar);
}
If you want a char * to a string regardless of whether UNICODE is defined, use the following:
#include <afx.h>
#include <stdio.h>
#include <winnt.h> // windows type definitions
#include <tchar.h>
void main( )
{
CString theString("This is a test" ); // converts to UNICODE string internally
char *mychar = new char[theString.GetLength()+1];
// strcpy(mychar, (const char *)theString); // class way
strcpy(mychar, (LPSTR)theString); // windows specific type cast
printf("mytchar=%s", mychar);
}
If UNICODE is defined the following might have a problem with this approach:
#include <afx.h>
#include <stdio.h>
void main()
{
CString theString( "This is a test" );
char* mychar = new char[theString.GetLength()+1];
_tcscpy(mychar, theString);
printf ("mychar= %s",mychar);
}
_tcscpy(_TCHAR *, const TCHAR *)
This would attempt to push a UNICODE string into a variable that is not supported for UNICODE. Memory space not large enough to hold the string.
James Sigler
Dallas, TX
JimDuncan
I guess I just needed a break and a pointer :)
smartwebagent
Hi Horus_Kol,
Here is a link that includes a sample as well of how to convert from CString to char:
http://msdn.microsoft.com/library/default.asp url=/library/en-us/vccore/html/_core_Strings3a_CString_Operations_Relating_to_C2dStyle_Strings.asp
The link lists a sample similar to doing something like:
#include <afx.h>
#include <stdio.h>
void main()
{
CString theString( "This is a test" );
char* mychar = new char[theString.GetLength()+1];
_tcscpy(mychar, theString);
printf ("mychar= %s",mychar);
}
Hope this helps!
Thanks,
Ayman Shoukry
VC++ Team