tags:

views:

360

answers:

5
+2  Q: 

CStringT to char[]

I'm trying to make changes to some legacy code. I need to fill a char[] ext with a file extension gotten using filename.Right(3). Problem is that I don't know how to convert from a CStringT to a char[].

There has to be a really easy solution that I'm just not realizing...

TIA.

+2  A: 

If you have access to ATL, which I imagine you do if you're using CString, then you can look into the ATL conversion classes like CT2CA.

CString fileExt = _T ("txt");
CT2CA fileExtA (fileExt);

If a conversion needs to be performed (as when compiling for Unicode), then CT2CA allocates some internal memory and performs the conversion, destroying the memory in its destructor. If compiling for ANSI, no conversion needs to be performed, so it just hangs on to a pointer to the original string. It also provides an implicit conversion to const char * so you can use it like any C-style string.

This makes conversions really easy, with the caveat that if you need to hang on to the string after the CT2CA goes out of scope, then you need to copy the string into a buffer under your control (not just store a pointer to it). Otherwise, the CT2CA cleans up the converted buffer and you have a dangling reference.

Nick Meyer
A: 

First use CStringA to make sure you're getting char and not wchar_t. Then just cast it to (const char *) to get a pointer to the string, and use strcpy or something similar to copy to your destination.

If you're completely sure that you'll always be copying 3 characters, you could just do it the simple way.

ext[0] = filename[filename.Length()-3];
ext[1] = filename[filename.Length()-2];
ext[2] = filename[filename.Length()-1];
ext[3] = 0;
Mark Ransom
A: 

I believe this is what you are looking for:

CString theString( "This is a test" );
char* mychar = new char[theString.GetLength()+1];
_tcscpy(mychar, theString);

If I remember my old school MS C++.

Adam W
This won't compile when UNICODE is defined. _tcscpy requires two buffers of TCHARs. If UNICODE is defined, you need to perform an actual character set conversion with WideCharToMultiByte.
Nick Meyer
Ah, fair enough. It's been quite a while since I used ATL or MFC.
Adam W
+2  A: 

Well you can always do this even in unicode

char str[4];
strcpy( str, CStringA( cString.Right( 3 ) ).GetString() );

If you know you AREN'T using unicode then you could just do

char str[4];
strcpy( str, cString.Right( 3 ).GetString() );

All the original code block does is transfer the last 3 characters into a non unicode string (CStringA, CStringW is definitely unicode and CStringT depends on whether the UNICODE define is set) and then gets the string as a simple char string.

Goz
This seems like the best solution for my case. Thanks!
Justin
A: 

You do not specify where is the CStringT type from. It could be anything, including your own implementation of string handling class. Assuming it is CStringT from MFC/ATL library available in Visual C++, you have a few options:

It's not been said if you compile with or without Unicode, so presenting using TCHAR not char:

CStringT
    <
    TCHAR,
    StrTraitMFC
        <
        TCHAR,
        ChTraitsCRT<TCHAR>
        >
    > file(TEXT("test.txt"));

TCHAR* file1 = new TCHAR[file.GetLength() + 1];
_tcscpy(file1, file);

If you use CStringT specialised for ANSI string, then

std::string file1(CStringA(file));
char const* pfile = file1.c_str(); // to copy to char[] buffer
mloskot