Permalink
All your code in one place
Notice that in C11 a string literal has type const char, and the conversion to char. (i.e. Pointer to non- const ) is illegal. This said: #include.
GitHub makes it easy to scale back on context switching. Read rendered documentation, see the history of any file, and collaborate with contributors on projects across GitHub.
Sign up for free See pricing for teams and enterprises Find file Copy path
Cannot retrieve contributors at this time
// This is a part of the Microsoft Foundation Classes C++ library. |
// Copyright (C) Microsoft Corporation |
// All rights reserved. |
// |
// This source code is only intended as a supplement to the |
// Microsoft Foundation Classes Reference and related |
// electronic documentation provided with the library. |
// See these sources for detailed information regarding the |
// Microsoft Foundation Classes product. |
///////////////////////////////////////////////////////////////////////////// |
// |
// Implementation of parameterized Array |
// |
///////////////////////////////////////////////////////////////////////////// |
// NOTE: we allocate an array of 'm_nMaxSize' elements, but only |
// the current size 'm_nSize' contains properly constructed |
// objects. |
#include'stdafx.h' |
#include<wchar.h> |
#include'elements.h'// used for special creation |
#definenew DEBUG_NEW |
///////////////////////////////////////////////////////////////////////////// |
staticvoid_ConstructElements(CString* pNewData, INT_PTR nCount) |
{ |
ASSERT(nCount >= 0); |
while (nCount--) |
{ |
ConstructElement(pNewData); |
pNewData++; |
} |
} |
staticvoid_DestructElements(CString* pOldData, INT_PTR nCount) |
{ |
ASSERT(nCount >= 0); |
while (nCount--) |
{ |
DestructElement(pOldData); |
pOldData++; |
} |
} |
staticvoid_CopyElements(CString* pDest, CString* pSrc, INT_PTR nCount) |
{ |
ASSERT(nCount >= 0); |
while (nCount--) |
{ |
*pDest = *pSrc; |
++pDest; |
++pSrc; |
} |
} |
///////////////////////////////////////////////////////////////////////////// |
CStringArray::CStringArray() |
{ |
m_pData = NULL; |
m_nSize = m_nMaxSize = m_nGrowBy = 0; |
} |
CStringArray::~CStringArray() |
{ |
ASSERT_VALID(this); |
_DestructElements(m_pData, m_nSize); |
delete[] (BYTE*)m_pData; |
} |
voidCStringArray::SetSize(INT_PTR nNewSize, INT_PTR nGrowBy) |
{ |
ASSERT_VALID(this); |
ASSERT(nNewSize >= 0); |
if(nNewSize < 0 ) |
AfxThrowInvalidArgException(); |
if (nGrowBy >= 0) |
m_nGrowBy = nGrowBy; // set new size |
if (nNewSize 0) |
{ |
// shrink to nothing |
_DestructElements(m_pData, m_nSize); |
delete[] (BYTE*)m_pData; |
m_pData = NULL; |
m_nSize = m_nMaxSize = 0; |
} |
elseif (m_pData NULL) |
{ |
// create one with exact size |
#ifdef SIZE_T_MAX |
ASSERT(nNewSize <= SIZE_T_MAX/sizeof(CString)); // no overflow |
#endif |
m_pData = (CString*) new BYTE[nNewSize * sizeof(CString)]; |
_ConstructElements(m_pData, nNewSize); |
m_nSize = m_nMaxSize = nNewSize; |
} |
elseif (nNewSize <= m_nMaxSize) |
{ |
// it fits |
if (nNewSize > m_nSize) |
{ |
// initialize the new elements |
_ConstructElements(&m_pData[m_nSize], nNewSize-m_nSize); |
} |
elseif (m_nSize > nNewSize) // destroy the old elements |
_DestructElements(&m_pData[nNewSize], m_nSize-nNewSize); |
m_nSize = nNewSize; |
} |
else |
{ |
// otherwise, grow array |
INT_PTR nGrowArrayBy = m_nGrowBy; |
if (nGrowArrayBy 0) |
{ |
// heuristically determine growth when nGrowArrayBy 0 |
// (this avoids heap fragmentation in many situations) |
nGrowArrayBy = min(1024, max(4, m_nSize / 8)); |
} |
INT_PTR nNewMax; |
if (nNewSize < m_nMaxSize + nGrowArrayBy) |
nNewMax = m_nMaxSize + nGrowArrayBy; // granularity |
else |
nNewMax = nNewSize; // no slush |
ASSERT(nNewMax >= m_nMaxSize); // no wrap around |
if(nNewMax < m_nMaxSize) |
AfxThrowInvalidArgException(); |
#ifdef SIZE_T_MAX |
ASSERT(nNewMax <= SIZE_T_MAX/sizeof(CString)); // no overflow |
#endif |
CString* pNewData = (CString*) new BYTE[(size_t)nNewMax * sizeof(CString)]; |
// copy new data from old |
Checked::memcpy_s(pNewData, (size_t)nNewMax * sizeof(CString), |
m_pData, (size_t)m_nSize * sizeof(CString)); |
// construct remaining elements |
ASSERT(nNewSize > m_nSize); |
_ConstructElements(&pNewData[m_nSize], nNewSize-m_nSize); |
// get rid of old stuff (note: no destructors called) |
delete[] (BYTE*)m_pData; |
m_pData = pNewData; |
m_nSize = nNewSize; |
m_nMaxSize = nNewMax; |
} |
} |
INT_PTR CStringArray::Append(const CStringArray& src) |
{ |
ASSERT_VALID(this); |
ASSERT(this != &src); // cannot append to itself |
if(this &src) |
AfxThrowInvalidArgException(); |
INT_PTR nOldSize = m_nSize; |
SetSize(m_nSize + src.m_nSize); |
_CopyElements(m_pData + nOldSize, src.m_pData, src.m_nSize); |
return nOldSize; |
} |
voidCStringArray::Copy(const CStringArray& src) |
{ |
ASSERT_VALID(this); |
ASSERT(this != &src); // cannot append to itself |
if(this != &src) |
{ |
SetSize(src.m_nSize); |
_CopyElements(m_pData, src.m_pData, src.m_nSize); |
} |
} |
voidCStringArray::FreeExtra() |
{ |
ASSERT_VALID(this); |
if (m_nSize != m_nMaxSize) |
{ |
// shrink to desired size |
#ifdef SIZE_T_MAX |
ASSERT(m_nSize <= SIZE_T_MAX/sizeof(CString)); // no overflow |
#endif |
CString* pNewData = NULL; |
if (m_nSize != 0) |
{ |
pNewData = (CString*) new BYTE[m_nSize * sizeof(CString)]; |
// copy new data from old |
Checked::memcpy_s(pNewData, m_nSize * sizeof(CString), |
m_pData, m_nSize * sizeof(CString)); |
} |
// get rid of old stuff (note: no destructors called) |
delete[] (BYTE*)m_pData; |
m_pData = pNewData; |
m_nMaxSize = m_nSize; |
} |
} |
///////////////////////////////////////////////////////////////////////////// |
voidCStringArray::SetAtGrow(INT_PTR nIndex, LPCTSTR newElement) |
{ |
ASSERT_VALID(this); |
ASSERT(nIndex >= 0); |
if(nIndex < 0) |
AfxThrowInvalidArgException(); |
if (nIndex >= m_nSize) |
SetSize(nIndex+1); |
m_pData[nIndex] = newElement; |
} |
voidCStringArray::SetAtGrow(INT_PTR nIndex, const CString& newElement) |
{ |
ASSERT_VALID(this); |
ASSERT(nIndex >= 0); |
if(nIndex < 0) |
AfxThrowInvalidArgException(); |
if (nIndex >= m_nSize) |
SetSize(nIndex+1); |
m_pData[nIndex] = newElement; |
} |
voidCStringArray::InsertEmpty(INT_PTR nIndex, INT_PTR nCount) |
{ |
ASSERT_VALID(this); |
ASSERT(nIndex >= 0); // will expand to meet need |
ASSERT(nCount > 0); // zero or negative size not allowed |
if(nIndex < 0 || nCount <= 0) |
AfxThrowInvalidArgException(); |
if (nIndex >= m_nSize) |
{ |
// adding after the end of the array |
SetSize(nIndex + nCount); // grow so nIndex is valid |
} |
else |
{ |
// inserting in the middle of the array |
INT_PTR nOldSize = m_nSize; |
INT_PTR nOverlapSize = min(nCount, nOldSize - nIndex); |
SetSize(m_nSize + nCount); // grow it to new size |
// destroy slots we are about to overwrite |
_DestructElements(&m_pData[m_nSize - nOverlapSize], nOverlapSize); |
// shift old data up to fill gap |
Checked::memmove_s(&m_pData[nIndex+nCount], (m_nSize-(nIndex+nCount)) * sizeof(CString), |
&m_pData[nIndex], (nOldSize-nIndex) * sizeof(CString)); |
// re-init the now-vacant slots we moved data from |
_ConstructElements(&m_pData[nIndex], nOverlapSize); |
} |
// insert new value in the gap |
ASSERT(nIndex + nCount <= m_nSize); |
} |
voidCStringArray::InsertAt(INT_PTR nIndex, LPCTSTR newElement, INT_PTR nCount) |
{ |
// make room for new elements |
InsertEmpty(nIndex, nCount); |
// copy elements into the empty space |
CString temp = newElement; |
while (nCount--) |
m_pData[nIndex++] = temp; |
} |
voidCStringArray::InsertAt(INT_PTR nIndex, const CString& newElement, INT_PTR nCount) |
{ |
// make room for new elements |
InsertEmpty(nIndex, nCount); |
// copy elements into the empty space |
while (nCount--) |
m_pData[nIndex++] = newElement; |
} |
voidCStringArray::RemoveAt(INT_PTR nIndex, INT_PTR nCount) |
{ |
ASSERT_VALID(this); |
ASSERT(nIndex >= 0); |
ASSERT(nCount >= 0); |
INT_PTR nUpperBound = nIndex + nCount; |
ASSERT(nUpperBound <= m_nSize && nUpperBound >= nIndex && nUpperBound >= nCount); |
if(nIndex < 0 || nCount < 0 || (nUpperBound > m_nSize) || (nUpperBound < nIndex) || (nUpperBound < nCount)) |
AfxThrowInvalidArgException(); |
// just remove a range |
INT_PTR nMoveCount = m_nSize - (nUpperBound); |
_DestructElements(&m_pData[nIndex], nCount); |
if (nMoveCount) |
{ |
Checked::memmove_s(&m_pData[nIndex], nMoveCount * sizeof(CString), |
&m_pData[nUpperBound], nMoveCount * sizeof(CString)); |
} |
m_nSize -= nCount; |
} |
voidCStringArray::InsertAt(INT_PTR nStartIndex, const CStringArray* pNewArray) |
{ |
ASSERT_VALID(this); |
ASSERT(pNewArray != NULL); |
ASSERT_KINDOF(CStringArray, pNewArray); |
ASSERT_VALID(pNewArray); |
ASSERT(nStartIndex >= 0); |
if(pNewArray NULL || nStartIndex < 0) |
AfxThrowInvalidArgException(); |
if (pNewArray->GetSize() > 0) |
{ |
InsertAt(nStartIndex, pNewArray->GetAt(0), pNewArray->GetSize()); |
for (INT_PTR i = 0; i < pNewArray->GetSize(); i++) |
SetAt(nStartIndex + i, pNewArray->GetAt(i)); |
} |
} |
///////////////////////////////////////////////////////////////////////////// |
// Serialization |
voidCStringArray::Serialize(CArchive& ar) |
{ |
ASSERT_VALID(this); |
CObject::Serialize(ar); |
if (ar.IsStoring()) |
{ |
ar.WriteCount(m_nSize); |
for (INT_PTR i = 0; i < m_nSize; i++) |
ar << m_pData[i]; |
} |
else |
{ |
DWORD_PTR nOldSize = ar.ReadCount(); |
SetSize(nOldSize); |
for (INT_PTR i = 0; i < m_nSize; i++) |
ar >> m_pData[i]; |
} |
} |
///////////////////////////////////////////////////////////////////////////// |
// Diagnostics |
#ifdef _DEBUG |
voidCStringArray::Dump(CDumpContext& dc) const |
{ |
CObject::Dump(dc); |
dc << 'with ' << m_nSize << ' elements'; |
if (dc.GetDepth() > 0) |
{ |
for (INT_PTR i = 0; i < m_nSize; i++) |
dc << 'nt[' << i << '] = ' << m_pData[i]; |
} |
dc << 'n'; |
} |
voidCStringArray::AssertValid() const |
{ |
CObject::AssertValid(); |
if (m_pData NULL) |
{ |
ASSERT(m_nSize 0); |
ASSERT(m_nMaxSize 0); |
} |
else |
{ |
ASSERT(m_nSize >= 0); |
ASSERT(m_nMaxSize >= 0); |
ASSERT(m_nSize <= m_nMaxSize); |
ASSERT(AfxIsValidAddress(m_pData, m_nMaxSize * sizeof(CString))); |
} |
} |
#endif//_DEBUG |
IMPLEMENT_SERIAL(CStringArray, CObject, 0) |
///////////////////////////////////////////////////////////////////////////// |
Copy lines Copy permalink
Active2 years, 5 months ago
How do I convert CString into const char *? I have tried everything found on the internet but I still cant convert them.
Please help.
Thank you.
ildjarn57.3k88 gold badges107107 silver badges192192 bronze badges
Chicko BuenoChicko Bueno17222 gold badges99 silver badges2424 bronze badges
5 Answers
CString casts to const char * directly
will print 'foo'
Newer version of MFC also support the GetString() method:
Gregor BrandtGregor Brandt
Short answer: Use the
CT2CA
macro (see ATL and MFC String Conversion Macros). This will work regardless of your project's 'Character Set' setting.Long answer:
- If you have the
UNICODE
preprocessor symbol defined (i.e., ifTCHAR
iswchar_t
), use theCT2CA
orCW2CA
macro. - If you don't (i.e., if
TCHAR
ischar
), CString already has an operator to convert tochar const*
implicitly (seeCSimpleStringT::operator PCXSTR
).
57.3k88 gold badges107107 silver badges192192 bronze badges
If your application is not Unicode, you can simple typecast to
const char *
. Use the GetBuffer()
method if you need a char *
that you can modify.If your application is Unicode and you really want a
Jonathan WoodJonathan Woodchar *
, then you'll need to convert it. (You can use functions like MultiByteToWideChar()
.)44k5959 gold badges203203 silver badges324324 bronze badges
First : define char *inputString; in your_file.h
Second : define in yourFile.cpp :CString MyString;
The last two sentences convert a CString variable to a char*; but be carefull, with CString you can hold millons of charcteres but with char* no. You have to define the size of your char* varible.
user7748179user7748179
I know it's late, but I couldn't use the marked-as-answer solution. I search all over the internet and nothing worked for me. I muscled through it to get a solution:
jokerjoker1,59122 gold badges1818 silver badges2525 bronze badges