Please help me on this Here i want to save the converted data into new po开发者_StackOverflow社区inters. But everytime the data is overwriting with most recent data. Please check my code
TCHAR nameBuffer[256]; //Globally Declared
void Caller()
{
TCHAR* ptszSecondInFile= QStringToTCharBuffer(userName);
TCHAR* ptszOutFile=QStringToTCharBuffer(Destinationfilename);
}
TCHAR *dllmerge::QStringToTCharBuffer( QString buffer )
{
memset(nameBuffer, 0, sizeof(nameBuffer));
#if UNICODE
_tcscpy_s(nameBuffer, _countof(nameBuffer), buffer.toUtf8());
#else
_tcscpy_s(nameBuffer, _countof(nameBuffer), buffer.toLocal8Bit());
#endif
_tprintf( _T( "nameBuffer %s\n" ), nameBuffer );
return nameBuffer;
}
I am gettting ptszSecondInFile and ptszOutFile both same answer. Is it possible to do with TCHAR* nameBuffer[256];
Seems you use the global variable nameBuffer in the QStringToTCharBuffer. Make it local... Or just copy the value of nameBuffer in the Caller between the two calls... otherwise the second call will overwrite the value of the global variable...
As a programming advice for the future: DO NOT USE GLOBAL VARIABLES UNLESS YOU REALLY HAVE TO!!! In this case you don't have to use it.
You need memory to hold each string that you need at once so a global buffer isn't going to work here. Either have your QStringToTCharBuffer
function allocate a new char buffer for each string or pass a char buffer to the function. I'd advise the second one as you're more likely to forget that a function allocates memory.
i.e:
TCHAR *dllmerge::QStringToTCharBuffer( QString buffer )
{
TCHAR* nameBuffer = new TCHAR[256];
memset(nameBuffer, 0, sizeof(nameBuffer));
#if UNICODE
_tcscpy_s(nameBuffer, _countof(nameBuffer), buffer.toUtf8());
#else
_tcscpy_s(nameBuffer, _countof(nameBuffer), buffer.toLocal8Bit());
#endif
_tprintf( _T( "nameBuffer %s\n" ), nameBuffer );
return nameBuffer;
}
vs:
void Caller()
{
const int maxSize = 256;
TCHAR szSecondInFile[maxSize];
TCHAR szOutFile[maxSize];
QStringToTCharBuffer( userName, szSecondInFile, maxSize );
QStringToTCharBuffer( Destinationfilename, szOutFile, maxSize );
}
dllmerge::QStringToTCharBuffer( QString buffer, TCHAR* pOutString, const int size )
etc.
精彩评论