CStringT to char []

I am trying to make changes to some legacy codes. I need to fill in char[] extthe file extension obtained with filename.Right(3). The problem is that I do not know how to convert from CStringTto char[].

There must be a very simple solution that I just don't understand ...

TIA.

+3
source share
5 answers

Well, you can always do this even in Unicode

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

If you know that you are not using Unicode, you can simply do

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

The entire source block of code wraps the last 3 characters in a string other than unicode (CStringA, CStringW is definitely Unicode, and CStringT depends on whether UNICODE is set), and then it gets the string as a simple char string.

+4

ATL, , , , CString, ATL CT2CA.

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

( Unicode), CT2CA , . ANSI, , . const char *, C.

, , , CT2CA , ( ). CT2CA , .

+5

CStringA, , char, wchar_t. (const char *), , strcpy - .

, 3 , .

ext[0] = filename[filename.Length()-3];
ext[1] = filename[filename.Length()-2];
ext[2] = filename[filename.Length()-1];
ext[3] = 0;
0

, , :

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

MS ++.

0
source

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

It was not said if you are compiling with or without Unicode, so when using TCHARnot char:

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

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

If you are using a CStringT specialized for ANSI string, then

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

Source: https://habr.com/ru/post/1714532/


All Articles